944,089 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2665
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 28th, 2007
0

school's back, and so am i :D

Expand Post »
Hey everyone, I'm back in C++ this semester; Last semester I went through Structured C++ (and passed with a 4.0; many thanks to all of you!) and this semester I'm in OOP.

I don't have sample code yet because I haven't started. But I was talking to a classmate, and one of our programming exercises wants us to write a function that will modify a point (i.e., (3, 2)) so that it will rotate it 90 degrees, clockwise. Is there an easier way to do this rather than 5 or 6 "If" statements?


Example scenario:

I input point x as 3 and point y as 2. I select the option to rotate the point by 90 degrees and should get x as 2 and y as -3.
Last edited by Duki; Aug 28th, 2007 at 5:41 pm.
Similar Threads
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Aug 28th, 2007
0

Re: school's back, and so am i :D

How do you rotate a point? A point is a point, there's no kind of transformation you can do until you have more than one.
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Aug 28th, 2007
1

Re: school's back, and so am i :D

Don't they teach simple geometry in schools any more?
http://mathworld.wolfram.com/RotationMatrix.html
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 28th, 2007
0

Re: school's back, and so am i :D

Click to Expand / Collapse  Quote originally posted by Hamrick ...
How do you rotate a point? A point is a point, there's no kind of transformation you can do until you have more than one.
I haven't taken a math class in many, many years, but from what I can recall, you rotate a point by drawing a vector starting at the origin and passing through the point, and then rotate that vector 90 degrees, in this case.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is online now Online
13,646 posts
since Feb 2002
Aug 28th, 2007
0

Re: school's back, and so am i :D

Click to Expand / Collapse  Quote originally posted by cscgal ...
I haven't taken a math class in many, many years, but from what I can recall, you rotate a point by drawing a vector starting at the origin and passing through the point, and then rotate that vector 90 degrees, in this case.
Yes that's right, but it assumes you're rotating about the origin Either way, the origin is a point, so, as hamrick said, you need two points in order to rotate something.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Aug 28th, 2007
0

Re: school's back, and so am i :D

I like to do problems that are new to me. I found a few lines of trigonometry did just dandy.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 28th, 2007
0

Re: school's back, and so am i :D

Thanks Dave; any tips as to which ones?

Hamrick: sorry I didn't clarify. There is an assumed point (i'm guessing) that is constant at (0,0).
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Aug 28th, 2007
0

Re: school's back, and so am i :D

x' = x * cosine(90) + y * sine(90)
y' = x * sine(90) - y * cosine(90)

that look right?

edit] i think these are the right ones:

rx = x * cos(90) + y * sin(90) ;
ry = y * cos(90) - x * sin(90) ;
Last edited by Duki; Aug 28th, 2007 at 10:46 pm.
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006
Aug 28th, 2007
0

Re: school's back, and so am i :D

C++ Syntax (Toggle Plain Text)
  1. point(double x_ = 0, double y_ = 0)
  2. : x(x_), y(y_),
  3. radius(std::sqrt(x * x + y * y)),
  4. angle(std::atan(y / x))
  5. {
  6. }
  7. void rotate(double radians)
  8. {
  9. angle = std::fmod(angle + radians, 2 * pi);
  10. x = radius * cos(angle);
  11. y = radius * sin(angle);
  12. }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 28th, 2007
0

Re: school's back, and so am i :D

Ok, here's how I am doing it; is this wrong?

  
c++ Syntax (Toggle Plain Text)
  1. #include <cmath>
  2. #include <iostream>
  3. using namespace std ;
  4.  
  5. int main ()
  6. {
  7. int x ;
  8. int y ;
  9. double angle ;
  10. int rx ;
  11. int ry ;
  12.  
  13. angle = 90 ;
  14. x = 3 ;
  15. y = 2 ;
  16. rx = x * cos(angle) + y * sin(angle) ;
  17. ry = y * cos(angle) - x * sin(angle) ;
  18.  
  19. cout << rx << " " << ry << endl ;
  20.  
  21. return 0 ;
  22. }

something is wrong, because for x i get 0 output, but when i do it in my calculator i get the right answer (2) !
Last edited by Duki; Aug 28th, 2007 at 10:59 pm.
Reputation Points: 817
Solved Threads: 32
Nearly a Posting Virtuoso
Duki is offline Offline
1,474 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: binary code
Next Thread in C++ Forum Timeline: please help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC