943,929 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 300
  • C++ RSS
Oct 25th, 2009
0

I need help :(

Expand Post »
I am very new to computer programming and am struggling with my Computer Science class...

I have been working on this program all week long...

My assignment is to ;

Write a float function that will have four float parameters. The four parameters represent two Cartesian points, 2 x's and 2 y's. Return the distance between the two points. It involves a square root.
Write a main function which will ask the user for 2 points (4 numbers), decide whether the distance between them is within 25 units ("hit"), exactly 25 units ("on the rim"), or more than 25 units away ("missed"). You must call the function written previously. Use the fewest possible number of if statements and comparisons to do this efficiently. Output this message on the console window (use "cout").

Then add in some code to use the first point entered as the center of a circle, and draw a circle that is 25 units in radius. Draw a small circle at the location of the other point. Output the classification of the distance on the graphics screen (look up the function "outtextxy".). The use of the word "units" does not imply any conversions needed, they are just 'spaces on the plane' or pixels in the graphics sense.

This is what I have gotten so far... I honestly have no idea how close I am to finishing up

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cmath>
  3. #include "graphics.h"
  4.  
  5. using namespace std;
  6.  
  7. float point (float x1, float x2, float y1, float y2);
  8. float point (float x1, float x2, float y1, float y2)
  9. {
  10. if (distance < 25.0)
  11. return "hit";
  12. else if (distance == 25.0,)
  13. return "on the rim";
  14. else if (distance > 25.0)
  15. return "miss";
  16. }
  17.  
  18. int main ()
  19. {
  20. float x1;
  21. float x2;
  22. float y1;
  23. float y2;
  24. float distance;
  25. float squareroot;
  26.  
  27. cout << "Please enter the cordinates for the first point.";
  28. cin >> x1 >> y1;
  29. cout << "Now enter the cordinates for the second point.";
  30. cin >> x2 >> y2;
  31.  
  32. squareroot = pow (float(x2-x1),2) + pow (float(y2-y1),2);
  33. distance = squareroot/squareroot;
  34.  
  35. cout<< point <<endl;
  36.  
  37. return 0;
  38.  
  39. }

Any ideas of what I should do?

Thanks -
~mason
Reputation Points: 10
Solved Threads: 1
Light Poster
UKmason is offline Offline
43 posts
since Oct 2009
Oct 25th, 2009
0
Re: I need help :(
http://www.daniweb.com/code/snippet216430.html

this might help you with your circle
Last edited by Shinedevil; Oct 26th, 2009 at 12:01 am.
Reputation Points: 8
Solved Threads: 4
Junior Poster in Training
Shinedevil is offline Offline
71 posts
since Nov 2008
Oct 26th, 2009
0
Re: I need help :(
Thanks a bunch, now I just need to find out how to get it to draw a circle
Reputation Points: 10
Solved Threads: 1
Light Poster
UKmason is offline Offline
43 posts
since Oct 2009
Oct 26th, 2009
0
Re: I need help :(
Your professor gave you a function free of charge called outtextxy( ). Worst case scenario, why not just call outtextxy() individually for every ascii character that you will to output on the dos console to draw your circle?

This may seem inefficient, but here is a way to at least make your code look 'better':
C++ Syntax (Toggle Plain Text)
  1. struct coord{
  2.  
  3. int x;
  4. int y;
  5. };
  6.  
  7. void draw_circle();
  8.  
  9. coord point[80];
  10.  
  11.  
  12. //This is where it sucks.. it's up to you to come up with this list of coordinates
  13. //through trial-and-error.
  14. point[0].x = 10; point[0].y = 15;
  15. point[1].x = 11; point[1].y = 14;
  16. point[2].x = 12; point[2].y = 14;
  17. point[3].x = 13; point[3].y = 13;
  18. point[4].x = 14; point[4].y = 13;
  19. ...
  20. ...
  21. ...
  22. point[79].x = 9; point[79].y = 16;
  23.  
  24.  
  25. void draw_circle()
  26. {
  27. for(int i=0; i<80; i++)
  28. {
  29. outtextxy(point[i].x, point[i].y);
  30. }
  31. }

Although this looks brutally painful, I believe this is an appropriate technique for your level of academics. This will involve a lot of trial-and-error (having to recompile and run your program after every 'guess' of where to plot the next point for your circle) but after you've done the hard part of getting all those coordinate positions, the rest is easy.

One thing that will ease the pain: buy some graph paper. Once you get your circle started, it will be quite easy to forecast the majority of the remainder of coordinates for your ascii circle. This will help you ensure that all points are roughly at a 25 unit radius.

Other alternatives would be to look online for information on how to draw an ascii circle... your professor would undoubtedly ask where you got this information as some of these functions are quite complex... specifically the Bresenham Circle.
Last edited by Clinton Portis; Oct 26th, 2009 at 1:07 am.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Oct 26th, 2009
0
Re: I need help :(
C++ Syntax (Toggle Plain Text)
  1. void draw_circle()
  2. {
  3. for(int i=0; i<80; i++)
  4. {
  5. outtextxy(point[i].x, point[i].y);
  6. cout << '*';
  7. }
  8. }
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Oct 26th, 2009
0
Re: I need help :(
Not exactly sure if this will help but the equation to a circle is :
C++ Syntax (Toggle Plain Text)
  1. //Cartesian coordinate
  2. X^2 + Y^2 = R^2
  3. //polar coordinate
  4. //R = radius
  5. sin^2(x)* R + cos^2(X) * R = 1
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is online now Online
3,864 posts
since Dec 2008

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: adding arrays
Next Thread in C++ Forum Timeline: validate float number





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


Follow us on Twitter


© 2011 DaniWeb® LLC