user1359417
user1359417

Reputation: 41

Simple Trigonometry?

EDIT - Thanks for all the answers everyone. I think I accidentally led you slightly wrong as the square in the picture below should be a rectangle (I see most of you are referencing squares which seems like it would make my life a lot easier). Also, the x/y lines could go in any direction, so the red dot won't always be at the top y boundary. I was originally going for a y = mx + b solution, but then I got stuck trying to figure out how I know whether to plug in the x or the y (one of them has to be known, obviously).

I have a very simple question (I think) that I'm currently struggling with for some reason. I'm trying to have a type of minimap in my game which shows symbols around the perimeter of the view, pointing towards objectives off-screen.

Anyway, I'm trying to find the value of the red point (while the black borders and everything in green is known):

enter image description here

It seems like simple trigonometry, but for some reason I can't wrap my head around it. I just need to find the "new" x value from the green point to the red point, then I can utilize basic math to get the red point, but how I go about finding that new x is puzzling me.

Thanks in advance!

Upvotes: 1

Views: 264

Answers (3)

Kendall Frey
Kendall Frey

Reputation: 44316

scale = max(abs(x), abs(y))
x = x / scale
y = y / scale

This is the simple case, for a square from (-1, -1) to (1, 1). If you want a different sized square, multiply the coordinates by sidelen / 2.

If you want a rectangle instead of a square, use the following formula. (This is another solution to the arbitrarily-sized square version)

scale = max(abs(x) / (width / 2), abs(y) / (height / 2))
x = x / scale
y = y / scale

Upvotes: 4

Adam Liss
Adam Liss

Reputation: 48290

Let's call the length of one side of the square l. The slope of the line is -y/x. That means, if you move along the line and rise a distance y toward the top of the square, then you'll move a distance x to the left. But since the green point is at the center of the square, you can rise only l/2. You can express this as a ratio:

    -y   -l/2
    ——— = ———
     x     d

Where d is the distance you'll move to the left. Solving for d, we have

    d = xl/2y

So if the green dot is at (0, 0), the red dot is at

(-l/2, xl/2y)
.

Upvotes: 2

Potatoswatter
Potatoswatter

Reputation: 137780

All you need is the angle and the width of the square w.

If the green dot is at (0,0), then the angle is a = atan(y/x), the y-coordinate of the dot is w/2, and therefore the x-coordinate of the dot is tan(1/a) * (w/2). Note that tan(1/a) == pi/2 - tan(a), or in other words the angle you really want to plug into tan is the one outside the box.

Edit: yes, this can be done without trig, too. All you need is to interpolate the x-coordinate of the dot on the line. So you know the y-coordinate is w/2, then the x-coordinate is (w/2) * x/y. But, be careful which quadrant of the square you're working with. That formula is only valid for -y<x<y, otherwise you want to reverse x and y.

Upvotes: 1

Related Questions