Archived

This topic is now archived and is closed to further replies.

Torquai

Finding angles of triangles

Recommended Posts

I have this circle (it''s actually a clock) and I need to know what time the user clicks at. So far my reasoning is as follows: 1. Split the clock in four pieces and find what part is clicked. 2. Make a triangle starting from center and to the point clicked. 3. Find the angle the user clicked (THIS IS THE PROBLEM) 4. Find that time the angle represents. I think I have got 1 and 2 ok, but my math is''nt that good so I have little clue how to handle point 3. I think I have to calc the hypotenuse and use this, but im not sure how. Hope someone can help me here. Thanks in advance. Thomas Leggett

Share this post


Link to post
Share on other sites
If you take a vector from the center to some known location (such as 12:00), and a vector from the center to the clicked point, the dot product of the two vectors is equal to the cosine of the angle from the first vector to the second. Note that you need to normalize your vectors to unit length before computing the dot product.

Normalize a vector (x,y):

length=sqrt(x*x + y*y)
x = x/length
y = y/length

Calculate a dot b:

dotprod = a.x * b.x + a.y * b.y

The angle in radians then is calculated as acos(dotprod).


Golem
Blender--The Gimp--Python--Lua--SDL
Nethack--Crawl--ADOM--Angband--Dungeondweller

Share this post


Link to post
Share on other sites
Good theory but a few questions first.
How are you managing the triangles inside the circle, this would leave you with a sector between the bounded arc and the hypotenuse of the intersecting triangle?
Will you be finding the triagle as a percentage of the circumference or by taking the line between to preset coordinates on the perimeter of the circle as one side of the triagle?

Finding the hypotenuse: this only works for triangles with a right angle, i.e. you would have to plot the line from the mouse coords down or across to the x,y axis of your circle (asuming the origin of the axis is in the centre of the circle) and then follow the plot the line from the mouse coords to the origin to then find the angle between the origin and the mouse coords. Tough stuff but possible. To calculate the hypotensue you use the formula, a squared + b squared = c squared, where a and b are the to sides you know and c is the hypotenuse. To find one of the other sides just rearrange the formula.

Share this post


Link to post
Share on other sites
in fact you can do this in one step...

assuming you have the x and y coordinates of the point clicked relative to the center of the clock (i.e. x=0 & y=0 at the center of the clock). you can calculate the angle like so:

angle = atan2(x, y)

simple huh? note the angle is in radians...

EDIT:

I forgot to mention that the angle will be relative to the positive x-axis, and in a counter-clockwise direction... so you'll need to do a bit of work to convert that into the actual time, but that should be pretty straight forward.

[edited by - SpaceDude on April 3, 2004 10:03:44 PM]

Share this post


Link to post
Share on other sites
Heres how I would do it using vectors (off the top of my head):

C = centre of clock
P = point clicked
O = 12 on the clock (vector representing the position of the 12)

if( |P-C| > |O-C| ) point is not inside the clock so return
//Make sure that the point is in the clock
//If O is the position of the 12 then the length of O-C is the
//radius of the clock

a = acos( (O-C).(P-C) ) * 180.0 / 3.146
//Gives the angle between 12 and where the user has pointed
//You dont really need to convert it, but I prefer to
//work with degrees

V = { -(O-C)y , (O-C)x }
if( P.V < 0 ) a = 180 - a + 180
//a is an angle between 0 and 180. If P is on the left of the O-//C then find out how much a extends from 180 and then add 180
//to it to get the actual angle from 0 to 360

Time = a / 30

That look right? (haven''t tested)

Share this post


Link to post
Share on other sites