Simple while loop doesn't end..

why does this not end...
It should end when either one of the values becomes 19 or above..


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
typedef std::pair<int,int> coordinate;
coordinate endpoints(double angle, coordinate start)
{
    double radians = (M_PI/180)*angle;
    double x2 = 0;
    double y2= 0;
    int lenght = 0;
    
    while (x2 < 19 ||  y2 < 19)
    {
        x2 = start.first + (lenght * cos(radians));
        y2 = start.second + (lenght * sin(radians));
        std::cout << x2 << y2 << std::endl;
        lenght++;
    }

    
    return std::make_pair(round(x2),round(y2));
}
You probably meant to use &&, not ||. As it is, the loop will only end when both x2 and y2 become greater than 19 (x2 < 19 || y2 < 19 is equivalent to !(x2 >= 19 && y2 >= 19)). If angle == 0 then x2 will always equal start.first.
i only have to end if one of being 19.. could i ask a related question to this ?
Last edited on
What happens when x2 or y2 are negative, and become more negative?
well.. That the case..


I have a problem with the negative value..
The idea with this code was to find an endpoint inside a matrix sixes 19x19.

from the center of this matrix (10,10) will a line with length 9 (hence x2 <19 or y2 < 19) try to find a endpoint at that given angle with the given length/displacement.

So sometimes i get negative values.. but at those cases it should output a proper value which fits the matrix, and is angled with the given angle from (10,10)
in am not sure i get it..?
Last edited on
Just ignore him. dhs2's trolling.
You don't need a loop to calculate that.

Let L be a straight line that forms an angle theta with the horizontal axis and passes through (10, 10), then its equation is y = cos(theta) * (x - 10) + 10 if theta in (-pi / 2; pi / 2). This line crosses these lines at the following points:
x = 0 at (0, cos(theta) * (-10) + 10) = P1
x = 19 at (19, cos(theta) * 9 + 10) = P2
y = 0 at (-10 / cos(theta) + 10, 0) = P3
y = 19 at (9 / cos(theta) + 10, 19) = P4
If P3.x > 19 || P3.x < 0 then the line is approximately horizontal, and will leave the box at P1 and P2.
If P3.x < 19 && P3.x > 0 then the line is approximately vertical, and will leave the box at P3 and P4.
If P3.x == 19 || P3.x == 0 the line is exactly diagonal and either pair of points may be used.
I'm not sure whether you want the maximum length which will fit within those boundaries. That would make a diagonal longer than a horizontal or vertical. Ot is it like the radius of a circle, a fixed length.

If you start from (10,10), 19 would be an upper limit. 1 would be the lower limit. You'd need to check both.
maybe?
1
2
3
4
5
6
    do {
        x2 = start.first + (length * cos(radians));
        y2 = start.second + (length * sin(radians));
        std::cout << std::setw(8) << x2 << std::setw(8) <<  y2 << std::endl;        
        length++;
    } while (x2<19 &&  y2<19 && x2>1 && y2>1);
The idea is has to be computed from 0 - 2pi..

in between is computed using the equation.. and x = 9..

what about -pi/4 and pi/4..


what about X in between?
Last edited on
@Chervill


19 is the fixed length (9 steps steps along either x or y ) the lower limit is 0.. array implementation..
Last edited on
How do you handle negative values?

I don't.

Perhaps I misunderstood. I used a starting point of 10, 10
I think this way the value can never become negative.

I'm not sure about the change from while to do-while. it was just an idea I had to make sure that it started off from x2 = start.first and y2 = start.second.
You don't need a loop to calculate that.

Yes, I agree.
The idea is has to be computed from 0 - 2pi..
The range [pi; 2*pi] is equivalent to the range [0; pi], only the order of the solutions is inverted. I.e. if f(theta) = sequence[a, b], then f(theta + pi) = sequence[b, a]. If order doesn't matter then f(theta) = f(theta + pi).

in between is computed using the equation.. and x = 9..
I don't understand this sentence.

what about -pi/4 and pi/4..
Those values are explicitly included in my method. Do you mean (+/-)pi/2? For (+/-)pi/2 the solutions are known: (10, 0) and (10, 19).
Last edited on

I am at the moment trying to iterate a 2d array [square matrix] using a line which scans the matrix with a given radius, and angle..

In this case the radius is 9 (9 being 9 different array position), and the angle being from 0 - 2pi.

The endpoint value should be a value within the 2d array, which was my first problem, and then the negative values occurred.

your solution seem to output values which is displaced less than 9 array positions array from the center...


0-90-180-270-360 seem to be correct.. There is some roundoff errors...

since you ignore negative values, you don't assure that the end positions is placed 9 array positions away from the start..
Last edited on
If you just want the matrix cell that is at a certain polar coordinate from the center, then that's simply
(round(r * cos(theta) + centerx), round(r * sin(theta) + centery)
After rounding, coordinates outside the matrix should be ignored.
Topic archived. No new replies allowed.