Roel Balink
Roel Balink

Reputation: 417

Find points on edge of screen with an angle and radius

I have got a square with the bounds of (0,0) to (800,600) In this raster I have to draw a line, given to me are the Angle and Radius of a line, starting from point (0,0). These bounds are based on a screen with a resolution of 800x600

I know how to calculate the (X,Y) points from this point, X = Radius * Cos(Angle) Y = Radius * Sin(Angle)

But from this point I don't know how to continue. In the images below I've defined my use cases. (X3,Y3) are the points I know how to calculate as told above. But how do I find (X1, Y1) and (X2, Y2)?

The Angle and Radius can be all kinds of values, see for example the values below. When both X3 and Y3 are a negative value I know the (X1,Y1) and (X2, Y2) will not fall in the raster.

Example 1

Example 2

Example 3

I think this isn't to dificult to calculate, but I just don't know how to do this.

Upvotes: 0

Views: 181

Answers (1)

SomeBody
SomeBody

Reputation: 8743

The line from the origin can be expressed by the equation

y = mx

where the slope m = y3/x3. You will need a line perpendicular to your first line, which means it has a slope of

m' = -1/m

Therefore it can be expressed by the equation

y = m'(x-x3) + y3

The point (X1,Y1) will have X1 = 0 and Y1 can be calculated via Y1 = m'x + y3. The point (X2,Y2) will have Y2 = 0 and X2 = (y - Y2)/m' + x3.

Upvotes: 1

Related Questions