#### Archived

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

# Finding angles of triangles

This topic is 5219 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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

##### 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 on other sites
I think arctan(x/y) should give the value you want and be about the easiest way to get it. (Not sure how fast arctan is, but for your purpose it's plenty fast )

http://mathworld.wolfram.com/Trigonometry.html has some detailed information

Drakonite

Shoot Pixels Not People

[edited by - drakonite on April 3, 2004 5:04:48 PM]

##### 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 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

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)

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 29
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631739
• Total Posts
3001958
×