9 Replies - 7994 Views - Last Post: 04 October 2011 - 10:24 AM Rate Topic: -----

#1 Donpa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-March 11

Problem finding the correct angle to rotate

Posted 04 October 2011 - 05:57 AM

Hello everyone,

I'm having a problem with finding the correct angle so i can rotate a image to face a point. (See image attached for example). In my program there is a arrow in the middle of the screen (900x900). I have all the code written to rotate the image correctly to a certain angle (if I pass 90 to it it'll rotate correctly to 90 degrees if I pass 270 it'll rotate to that.. etc). The problem I having is that I can't seem to get the correct angle to pass to my function. I am trying to find the angle realitive to the center of the screen (450,450)(red dots in picture). My code is on my development computer so I can't copy & paste it, but I'll explain my process with some code. First I multiple the magnitude of both points and divide the dot product of the points by that value.

// Example code (Not actual Code)

Point p1 = new Point(450,450);
Point p2 = new Point(900,900);


value = dotProduct(p1,p2) / (getMagnitude(p1) * getMagnitude(p2));




Then I use -cos to get the line angle in radians then convert it into a angle;
radian = Math.Acos(value);

angle = radian * (180 / Math.PI);



The angle I recieve from performing the code above is 90. Which isn't what I need nor is it right. On a normal grid shouldn't that be 45 degrees (p2 would be up and to the right on the grid)? On the screen wouldn't it be 135 (top left corner = 0,0 & bottom right is 900,900 so the line would angle down and to the right from the center).

I'm not really sure what is the problem. I can only assume I am going about it the wrong way. Anybody have any ideas?

Attached image(s)

  • Attached Image

This post has been edited by Donpa: 04 October 2011 - 05:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem finding the correct angle to rotate

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Problem finding the correct angle to rotate

Posted 04 October 2011 - 08:35 AM

Relative to the *screen* is pointless since the rest of your references are not relative to the screen. Everything is relative to the image you are working with.

Quote

On a normal grid shouldn't that be 45 degrees (p2 would be up and to the right on the grid)? On the screen wouldn't it be 135 (top left corner = 0,0 & bottom right is 900,900 so the line would angle down and to the right from the center).


I'm still a little unclear on what criteria you are using for right and wrong, but putting that aside. 45 degrees from center is still 45 degrees from center.

I can't at all figure out how you think a line at 45 degrees (pointing to the 1:30 position on a clock) is somehow supposed to be, or going to be the same as a line at 135 degrees (pointing to the 4:30 position on a clock).

I'm not sure why you are thinking this line is somehow different because it is in an image. If the center is your 0,0 just do the math from that point.
Was This Post Helpful? 0
  • +
  • -

#3 Donpa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-March 11

Re: Problem finding the correct angle to rotate

Posted 04 October 2011 - 09:01 AM

I'll try to clarify. I need to find a way to rotate the image in the center of the screen at origin(0,0) to face a point on a screen. I already made a function that will rotate the image based on a passed angle. So if I tell it to rotate 270 degrees it will face the left dot on my image (attached above), but what if I didn't know how many degrees to rotate the image to face that point. How would I go about that finding that angle/degree?

To simplify the concept think of a clock at 12. How many degrees would you have to rotate the hand for it to face 7. Does that make a little more sense?
Was This Post Helpful? 0
  • +
  • -

#4 Donpa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-March 11

Re: Problem finding the correct angle to rotate

Posted 04 October 2011 - 09:13 AM

View PosttlhIn`toq, on 04 October 2011 - 08:35 AM, said:

I can't at all figure out how you think a line at 45 degrees (pointing to the 1:30 position on a clock) is somehow supposed to be, or going to be the same as a line at 135 degrees (pointing to the 4:30 position on a clock).


Don't worry about it. I was wrong. For some reason I got mixed up. Yes it is 90 degrees but it wasn't the value I needed. I was thinking of rotating from 0. Rotating from 0, 90 degrees would bring me to 90 not 45 or 135 which is what I was looking for.
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Problem finding the correct angle to rotate

Posted 04 October 2011 - 09:13 AM

So correct me if I'm wrong. You're saying that you have an arbitrary point. You want to find out how many degrees to rotate so that your line is aiming at the point?
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Problem finding the correct angle to rotate

Posted 04 October 2011 - 09:15 AM

Rotation is always done around the center.

I think you are confusing yourself by trying to take the .Location property into account in some way.

Just .Location of 0,0 being in the upper left corner has nothing to do with a common mathematical grid starting with the center at 0,0

You should be able to see this with your existing testing. If you rotate 90 degrees the rotation takes place based on the center, right?

This post has been edited by tlhIn`toq: 04 October 2011 - 09:17 AM

Was This Post Helpful? 0
  • +
  • -

#7 Donpa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-March 11

Re: Problem finding the correct angle to rotate

Posted 04 October 2011 - 09:34 AM

View PostCurtis Rutland, on 04 October 2011 - 09:13 AM, said:

So correct me if I'm wrong. You're saying that you have an arbitrary point. You want to find out how many degrees to rotate so that your line is aiming at the point?


Yes, but it has to be the point of the arrow facing.

tlhIn`toq said:

Rotation is always done around the center.

I think you are confusing yourself by trying to take the .Location property into account in some way.

Just .Location of 0,0 being in the upper left corner has nothing to do with a common mathematical grid starting with the center at 0,0

You should be able to see this with your existing testing. If you rotate 90 degrees the rotation takes place based on the center, right?


I think your getting hung up on the wrong part. Of course rotation is done around a center. In the "example" I simply used 450,450 which would be the center of a 900x900 grid. If you have an arrow facing upward say for example at 0/360 degrees you would have to rotate 135 degrees to point at 900 x 900. Now the problem I have is how do you find that degree number dynamicly. Random point A, and random point B. Have point B face point A. How would you determine how many degrees to rotate B.

This post has been edited by Donpa: 04 October 2011 - 09:35 AM

Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Problem finding the correct angle to rotate

Posted 04 October 2011 - 09:46 AM

By "face point A" do you mean have point B.X = A.X, in other words rotate the entire image around point A until point B lines up directly above it?

It's amazing what you find when you Google
Terms: "C# Calculate angle between two points"
Hits: Lots
Example: This one with code.
http://www.carlosfem...ts-using-C.aspx


Finding answers to specific problems:
Sometimes just knowing where to look can make all the difference. Google is your friend.
Search with either "C#" or "MSDN" as the first word: "MSDN Picturebox", "C# Custom Events", "MSDN timer" etc.
How to do a good search that will get you targeted answers.

This post has been edited by tlhIn`toq: 04 October 2011 - 09:48 AM

Was This Post Helpful? 0
  • +
  • -

#9 Donpa   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-March 11

Re: Problem finding the correct angle to rotate

Posted 04 October 2011 - 10:15 AM

View PosttlhIn`toq, on 04 October 2011 - 09:46 AM, said:

By "face point A" do you mean have point B.X = A.X, in other words rotate the entire image around point A until point B lines up directly above it?

It's amazing what you find when you Google
Terms: "C# Calculate angle between two points"
Hits: Lots
Example: This one with code.
http://www.carlosfem...ts-using-C.aspx


Finding answers to specific problems:
Sometimes just knowing where to look can make all the difference. Google is your friend.
Search with either "C#" or "MSDN" as the first word: "MSDN Picturebox", "C# Custom Events", "MSDN timer" etc.
How to do a good search that will get you targeted answers.



Yeah... you might want to read the links you post. The code in that is terrible.

Well, I got it to work anyway. Was a simple fix of actually using the function I updated the other day. Stupid mistakes always take the longest to fix. I'll post the solution later.
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Problem finding the correct angle to rotate

Posted 04 October 2011 - 10:24 AM

View PostDonpa, on 04 October 2011 - 11:15 AM, said:

Yeah... you might want to read the links you post. The code in that is terrible.


As the person looking for help, that's sort of up to you. Don't expect others to do all the research and take an hour to pre-filter the results for you. Its your homework. You know better than us which of the many examples on the internet best suite your needs and your coding style.

View PostDonpa, on 04 October 2011 - 11:15 AM, said:

Well, I got it to work anyway. Was a simple fix of actually using the function I updated the other day. Stupid mistakes always take the longest to fix. I'll post the solution later.


Getting it going is the important thing. Yes, debugging is always far more than half the time spent on a project sometimes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1