473,320 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Math - Trig Problem in C

Jezternz
145 100+
Hi.

Ok basicly I am working on something using C# XNA and I cant get my head around it, possibly a simple answer I dont know.

I have a circle with a center point, co-ordinates (0,0).
The Radius could be any amount (we do know what it is though)
I have a point (which can be anywhere - is variable), we do know the co-ordinates but they could be anywhere around the circle.
We know the degrees (read below)

I want to move around the circle from the point by an amount of degrees and find the new point. Im not sure how clear this is so I will draw up a quick image.

Now in my image here, you see my problem I need the co-ordinates of the new point. In picture(4) I have shown which angles and lengths I think I can get using standard Trig/pythagorus, however its the green lengths I need. With the green lengths I can just add these too the co-ordinates of the origonal point to get new point coords. any ideas? alternative ways to get what I need?

Thanks, Josh
Sep 5 '08 #1
14 2180
JosAH
11,448 Expert 8TB
A rotation matrix is all you need.

kind regards,

Jos
Sep 5 '08 #2
Jezternz
145 100+
A rotation matrix is all you need.

kind regards,

Jos
I have only recently started using matrices so im still a bit new with them. Would you be able to start of the algorythm?
can offcourse assume we have variables:
oldX
oldY
AngleChange

I will try better understand it anyway, so thanks.

Josh
Sep 5 '08 #3
JosAH
11,448 Expert 8TB
I have only recently started using matrices so im still a bit new with them. Would you be able to start of the algorythm?
can offcourse assume we have variables:
oldX
oldY
AngleChange

I will try better understand it anyway, so thanks.

Josh
It's just a matrix multiplication:

Expand|Select|Wrap|Line Numbers
  1. double oldx, oldy; // assume they have certain values;
  2. double cosf= cos(f); // f is the rotation angle;
  3. double sinf= sin(f);
  4. double newx= cosf*oldx-sinf*oldy; // rotate oldx
  5. double newy= sinf*oldx+cosf*oldy; // rotate oldy
  6.  
That's all there is to it.

kind regards,

Jos
Sep 5 '08 #4
donbock
2,426 Expert 2GB
Am I missing something, or is the circle (and its radius) irrelevant?

Regarding the rotation matrix, there are a few details you need to worry about:
  1. The origin for (oldx,oldy) has to have the correct relationship to the center of rotation.
  2. Try a few examples to make sure you have the right sense for which directions are positive x and y.
  3. Which direction is positive rotation angle (either specified in your problem definition or up to you).
  4. Try a few examples to see if the equations have the proper sense for the angle value. That is, do you need to replace all instances of "f" with "-f"?

Cheers,
donbock
Sep 5 '08 #5
JosAH
11,448 Expert 8TB
Am I missing something, or is the circle (and its radius) irrelevant?

Regarding the rotation matrix, there are a few details you need to worry about:
  1. The origin for (oldx,oldy) has to have the correct relationship to the center of rotation.
  2. Try a few examples to make sure you have the right sense for which directions are positive x and y.
  3. Which direction is positive rotation angle (either specified in your problem definition or up to you).
  4. Try a few examples to see if the equations have the proper sense for the angle value. That is, do you need to replace all instances of "f" with "-f"?

Cheers,
donbock
According to the OP the centre of the origin is 0,0. The radius of the circle has
been taken care of by oldx,oldy. The coordinate system is the ususl mathematical
coordinate system (Y pointing upwards, X pointing to the right). The angle of
rotation also is a standard one: counter clockwise. There is no need to test
anything because this is math 101..

kind regards,

Jos
Sep 5 '08 #6
arnaudk
424 256MB
Am I missing something, or is the circle (and its radius) irrelevant?
The origin was given to be at (0,0) so since the point (oldx,oldy) is on the circle Pythagoras tells you the radius is then sqrt(oldx^2 + oldy^2), not that you need it to work out the rotation.
Sep 5 '08 #7
arnaudk
424 256MB
Hey, you bet me this time, Jos. That's a first. :D
Sep 5 '08 #8
JosAH
11,448 Expert 8TB
Hey, you bet me this time, Jos. That's a first. :D
Yep, by at least one minute (and still speeding up ;-)

kind regards,

Jos ( <--- takes care of his melting keyboard)
Sep 5 '08 #9
donbock
2,426 Expert 2GB
The coordinate system is the ususl mathematical
coordinate system (Y pointing upwards, X pointing to the right). The angle of
rotation also is a standard one: counter clockwise. There is no need to test
anything because this is math 101..
I knew that, I was just suggesting that the original questioner should verify for himself that the equations you gave work for his coordinate system and convention for positive angles of rotation.

Regarding the radius of the circle; I didn't notice that the (oldx,oldy) point was always on the circle. Whoops!

Cheers,
donbock
Sep 5 '08 #10
Jezternz
145 100+
well basicly I have a triangle rendered in the center, however when I run the new formulae:

float oldx = camposx, oldy = camposy;
camposx = ((float)Math.Cos(camturnangle) * oldx) - ((float)Math.Sin(camturnangle) * oldy);
camposy = ((float)Math.Sin(camturnangle) * oldx) + ((float)Math.Cos(camturnangle) * oldy);

note camposy, camposx are the positions of a camera in XNA (always faces 0,0,0) so basicly just treat the camera like the point going around the outside.
Starts like:

float camturnangle = 0.1f;
float camposx = 0f;
float camposy = 50f;
float camposz = 50f;

However on runtime, it just does a funny jiggle around and around, it does not actually rotate around the whole triangle. I am not sure if this is because of the start position or something. Note this is in 3 dimensions, I just want it to rotate around using xy, should z be something else?

Thanks for your help so far guys.
Sep 6 '08 #11
JosAH
11,448 Expert 8TB
Just to be sure: the angle for the sine and cosine functions is measured in radians, not degrees.

kind regards,

Jos
Sep 6 '08 #12
Jezternz
145 100+
oh cool did not realise that. My math is a bit rusty.
This is what i vaiguly remember?

Radian = 180 / Pie (degrees)

Mabey I am far off enlighten me.

Thanks again for your help.


Edit: Looked on wikipedia, See what it is now, thanks.
Sep 7 '08 #13
donbock
2,426 Expert 2GB
This is what i vaiguly remember?
Radian = 180 / Pie (degrees)
The following rant is not about C, so I hope the moderator will indulge me. I want to take a moment to describe a mathematical technique that I have relied on for 40 years. I'm an engineer, so I may say a few things that a mathematician would say aren't strictly true; but the calculations work out as if they were true so please bear with me.

So, for example, you want to convert degrees to radians. Find some number of degrees that are equal to some other number of radians: there are 360 degrees in a circle and there are 2pi radians in a circle. Write an equation:
360 degrees = 2pi radians

Anything divided by itself is 1; therefore since 360 degrees is the same as 2pi radians we can say the following. These are both "unit ratios".
Expand|Select|Wrap|Line Numbers
  1. (360 degrees) / (2pi radians) = 1
  2. (2pi radians) / (360 degrees) = 1
Suppose I want to convert 30 degrees to radians. I can multiply a number by 1 (that is, by a unit ratio) without changing its value; so ...
Expand|Select|Wrap|Line Numbers
  1.      30 degrees = (30 degrees) * ((2pi radians) / (360 degrees))
Rearranging the right-side terms and cancelling "degrees" yields ...
Expand|Select|Wrap|Line Numbers
  1.      30 degrees = (30 * 2pi / 360) radians
I chose the unit ratio that cancels out the units I don't want and leaves me with the units I do want.

You don't have to remember a bunch of conversion formulas; all you have to memorize are the relevant unit ratios. The next example shows that you can chain unit ratios together to derive whatever conversion factor you need.

Suppose I want to convert 10 miles/hour to meters/second ...
Expand|Select|Wrap|Line Numbers
  1. 10 miles/hour = (10 miles/hour) * (5280 feet/mile) * (12 inches/foot) *
  2.          (2.54 cm/inch) * ((1 meter) / (100 cm)) *
  3.          ((1 hour) / (60 minutes)) * ((1 minute) / (60 seconds))
This technique has to be applied with care if the zero-point in one set of units is different than the zero-point in the other set of units. For example, converting from degrees-Centigrade to degrees-Fahrenheit.
Suppose I want to convert 40 degrees-Centigrade (deg-C) to degrees-Fahrenheit (deg-F).
The span from water freezing to water boiling is
Expand|Select|Wrap|Line Numbers
  1. (100 deg-C) - (0 deg-C)  = 100 deg-C.
  2. (212 deg-F) - (32 deg-F) = 180 deg-F.
So we have the following unit ratios:
Expand|Select|Wrap|Line Numbers
  1. (100 deg-C) / (180 deg-F) = 1
  2. (180 deg-F) / (100 deg-C) = 1
40 deg-C is also 40 deg-C above the freezing point of water (a.F.P.H2O).
Expand|Select|Wrap|Line Numbers
  1. (40 deg-C) = (40 deg-C a.F.P.H2O) =
  2.      (40 deg-C a.F.P.H20) * ((180 deg-F) / (100 deg-C)) =
  3.      (40 * 180 / 100) deg-F a.F.P.H20
The freezing point of water is 32 deg-F, so
Expand|Select|Wrap|Line Numbers
  1. (40 * 180 / 100) deg-F a.F.P.H20 =
  2.      ((40 * 180 / 100) deg-F) + (32 deg-F)
Despite the length of my post, this is a very terse explanation. I hope you find it useful.

Cheers,
donbock
Sep 7 '08 #14
Jezternz
145 100+
wow. I had a basic understanding of how it all worked. That really had depth.
Thanks for the enlightenment.

Josh
Sep 7 '08 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Frank Millman | last post by:
Hi all I was helping my niece with her trigonometry homework last night. Her calculator's batteries were flat, so I thought I would use Python's math module to calculate sin, cos, and tan. I...
89
by: Radioactive Man | last post by:
In python 2.3 (IDLE 1.0.3) running under windows 95, I get the following types of errors whenever I do simple arithmetic: 1st example: >>> 12.10 + 8.30 20.399999999999999 >>> 1.1 - 0.2...
17
by: cwdjrxyz | last post by:
Javascript has a very small math function list. However there is no reason that this list can not be extended greatly. Speed is not an issue, unless you nest complicated calculations several levels...
4
by: John B. | last post by:
I'm self teaching myself C on a Linux box but I can't get a simple program to recognize math functions. I start the program with: #include <stdio.h> #include <math.h> but when I...
5
by: Tom Gurath | last post by:
http://osnews.com/story.php?news_id=5602&page=2 This benchmark tests the Math & File I/O of 9 languages/run-times. Visual C++ (Version 7 - not managed) Visual C# gcc C Visual Basic.NET Visual...
7
by: Mark Healey | last post by:
Do the trig functions in math.h work in degrees, radians or what? For some reason it doesn't say which in "man math.h" IIRC the arctan of a slope gives the angle. So, shouldn't atanf((float)1)...
15
by: Morgan Cheng | last post by:
Hi, I am writing a program that will take a lot of Math.Cos & Math.Sin operation. I am afraid this will be source of performance impact. Anybody knows how Math.cos & Math.Sin is implemented?...
6
by: Laserson | last post by:
Hi ! Can anybody give a code that performs calculating of sine function... I need it for report
0
by: Jon Harrop | last post by:
xahlee@gmail.com wrote: This does not even start running in Mathematica 6, let alone produce any correct results. The first line was deprecated some time ago. I don't know what is wrong with the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.