1. ## Goniometry problem

Hi,

I'm stuck with the following problem.

I have a 2D grid of size 600x600.
If I draw a line from a specific point (say 250,400) in a specific direction (say 100 degrees), I want to calculate on which point this line will cross the border of the grid.

I've been struggling with tangens and more of this stuff, but I can't seem to find the right formula... Hopefully someone can help me :-)

(if it's not possible with one formula, that's no problem, multiple formulas are okay too)

2. What's Goniometry ?

Place a corner of grid at origin, 2 sides of grid being x-axis and y-axis.
Take point (2,3); that point is 8 from right side of grid, 7 from top side: see that?

Walk (or draw line) from (2,3) going north-east at 40 degrees:
can you "see" a right triangle with angles 90-50-40?
Carry on...

3. Yes I know how to calculate it from one point.
Thing is, I need to be able to calculate it from every single point and every direction.

Problem is: it can cross the border at the top, right, bottom and left side. That's where I get stuck.

(in Dutch "goniometrie" is calculation of triangles, sinus, cosinus, etc. I thought in English it would be "goniometry" but apparantly not ;-)

4. In English, it's got just as silly a name: trigonometry!

So what you have is, if put simply:
1: a square : A(0,k), B(k,k), C(k,0) and D(0,0)
2: a point : P(x,y) INSIDE ABCD, so k > x > 0 and k > y > 0
3: a straight line from P to another point Q(a,b) on AB or BC or CD or DA
4: Q's coordinates are (depending on GIVEN angle):
if on AB: (a,k) ; if on BC: (k,b) ; if on CD: (a,0) ; if on DA: (0,b)

On the GIVEN angle:
1: trivial case: if 0, then Q(k,y); if 90, then Q(x,k); if 180, then Q(0,y); if 270, then Q(x,0)
2: similarly, if P(k/2,k/2), angles 45, 135, 225 and 315 will hit corners of square ABCD

Let us know IF that's what you mean; I think it is...

5. I'm not sure that's exactly what I mean.

What I have is:
1: a square : A(0,0), B(600,0), C(600,600), D(0,600)
2: a point : P(x,y) inside ABCD, so 600 > x > 0 and 600 > y > 0
3: an angle

I want to calculate point Q, which is on AB, BC, CD or DA or one of the corners.

What you say about these angles 45, 135, 225 and 315, they will only hit the corners if P = 300,300. But on other points they won't.

6. Originally Posted by mrfreeze
I'm not sure that's exactly what I mean.

What I have is:
1: a square : A(0,0), B(600,0), C(600,600), D(0,600)
2: a point : P(x,y) inside ABCD, so 600 > x > 0 and 600 > y > 0
3: an angle

I want to calculate point Q, which is on AB, BC, CD or DA or one of the corners.

What you say about these angles 45, 135, 225 and 315, they will only hit the corners if P = 300,300. But on other points they won't.
So I assume the angle is $\displaystyle \displaystyle0 \le \theta < 2\pi$ such that 0 means due east, $\displaystyle \displaystyle\frac{\pi}{2}$ means due north, etc.

For any point P(x,y), you can calculate the angle to get to each of the four corners using arctangent. Then you will be able to tell from your angle where Q is: on one of the four corners, or on exactly one of the four sides of the square (you will know which corner or side). If Q is on exactly one of the four sides, use the point-slope equation for the line PQ (use tangent to get the slope) and either set y=600, y=0, x=0, or x=600. If the angle is $\displaystyle \displaystyle\frac{\pi}{2}$ or $\displaystyle \displaystyle\frac{3\pi}{2}$, you can't use point-slope because slope is not defined, but it will be trivial.

EDIT: I see the original post mentions degrees rather than radians. It is simple enough to convert between them.

7. Originally Posted by mrfreeze
What I have is:
1: a square : A(0,0), B(600,0), C(600,600), D(0,600)
2: a point : P(x,y) inside ABCD, so 600 > x > 0 and 600 > y > 0
3: an angle
I want to calculate point Q, which is on AB, BC, CD or DA or one of the corners.
What you say about these angles 45, 135, 225 and 315, they will only hit the corners if P = 300,300. But on other points they won't.
You're correct; I forgot to specify point P has to have coordinates (k/2, k/2); correction EDITED.

So what I've done is simply replace your 600 with k: to make it general.
You have: a square : A(0,0), B(600,0), C(600,600), D(0,600)
I made it : a square : A(0,0), B(k,0), C(k,k), D(0,k) ; I had D at (0,0) but that's unimportant.

Are you in agreement with my previous post now?

8. Hi mrfreeze,

This statement is clear. you must define the angles. Are these the angles created by rotating a clock hand counter clockwise. I assume they are. You can define the slope and the line by drawing the appropriate slope diagram and using tangent property

bjh

9. Originally Posted by mrfreeze
If I draw a line from a specific point (say 250,400) in a specific direction (say 100 degrees).......
As bjhopper told you, "100 degrees" would mean 80 degrees heading North West; got that?

Similarly, "220 degrees" would mean 40 degrees heading South West.
"280 degrees" = 80 heading South East.