Beginners Task #3

Online C++ programming contests.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard, raimo

Postby leas5040 » Fri Jun 25, 2004 8:15 am

Just a tip here:
Kramer55 wrote:
heres the eqation of a circle

(x-h) ^2 + (y - k) ^ 2 = r^2

h is the x coodinate for the radius, same for k, except its the y coordinate for the radius

if the equation is (x-5) ^ 2 + (y+1)^2 = 16
center is (5,-1) radius is 4 (really +- 4)


Try writing the equation like this:

((x-h)^2)/r^2 + ((y - k)^2)/r^2=1

This will be much easier for you when you get to conic sections, with this you can tell instantly whether it is a circle or an ellipse.

And the radius is really just 4. You can't have a negative radius.
"Given enough time, man can do anything with a bit of string and some Tinker toys." Bruce Bolden, Senior Instructor at the University of Idaho.
User avatar
leas5040
 
Posts: 1214
Joined: Mon Apr 12, 2004 9:51 pm
Location: Moscow, ID

Postby tougo » Fri Jun 25, 2004 10:04 am

did anyone found out how the calculate the side of the best fitted square?
a simple yes or no will do...iam just curious..because it's the easier part of all

I like the fact that some are thinking and are investigating different ways to solve the problem. but keep it simple

I like the following sentence (the book was a reprint ..i do not really know when he first said that)

“Beauty is the first test: there is no permanent place in this world for ugly mathematics” (Hardy 1994).
tougo
 
Posts: 432
Joined: Sat Oct 04, 2003 9:18 am
Location: Athens / Greece

Postby RaH » Fri Jun 25, 2004 10:55 am

I need a shove in the right direction. I have read about the equation to find a circle. Well I have whipped this out so far. I'm sure I can use this somehow, but I know my math is off for this task. The program does well, handles negative values and even floats for the radius and diameter. Even if I have don't complete the task I feel that I have made progress for learning this much. :)
Code: Select all
/* Task #3 Take Input From The User, Of The Radius Of A
   Circle And An Initial (x,y), Calculate Several Points Around The
   Circle From The Initial Point, Then Calculate The Largest Square
   That Will Fit Inside The Circle, And Plot Several Points
*/
#include <iostream>
#include <math.h>

double long radius;
signed int x, y;

float CircFunc(double long);            // To Calculate The Circumference Of The Circle
int PlotCircPoints(int, int, float);    // To Plot Circle Points
//int PlotSquarePoints(int, int, float);  // To Plot Square Points

int main()
{
   cout << "Enter the radius of your circle: ";
   cin >> radius;

   cout << "Enter the starting (X,Y) coordinates: ";
   cin  >> x >> y;

   cout << "The circumference of a circle with a radius of " << radius << " is " << CircFunc(radius) << endl;
   PlotCircPoints(x, y, radius);
   return 0;
}


float CircFunc(double long)
{
   float diameter;
   float circ;
   float pi = 3.1416;

   diameter = radius * 2;  // *NOTE* Could I Do This  And Lose This Line?
   circ = diameter * pi;     //  *NOTE* circ = (radius * 2) * pi;
   return circ;
}

int PlotCircPoints(signed int, signed int, float)
{
   signed int x2, y2;   // Opposite Point Of Users Point
   int angleX, angleY;

   x2 = x;
   y2 = y;
   cout << "P1 (" << x << "," << y << ")\n";

   angleX = sin(45);
   angleY = cos(45);
   cout << angleX << " " << angleY << endl;

   // Find The Origin
   x = x - x2;
   y = y - y2;

   // Graph The Points To Make A Line

   for(int i = 2; i <= 5; i++)
   {
      cout << "P" << i << " (" << x << "," << y << ")\n";
      x = x - x2;
      y = y - y2;
   }
   return x, y;
}


/* OUTPUT
Enter the radius of your circle: 3
Enter the starting (X,Y) coordinates: 2 2
The crcumference of a circle with a radius of 3 is 18.8496
P1 (2,2)
0, 0
P2 (0,0)
P3 (-2,-2)
P4 (-4,-4)
P5 (-6,-6)


It takes the radius entered by the user, calculates the circumference of the circle. Takes coordinates entered by the user, then Plots points that make a straight line. So at this point I am happy where it is, but I would like some pointers/ tips on how I can make it better.
"Codito ergo sum"
RaH
 
Posts: 258
Joined: Mon May 24, 2004 11:59 am
Location: Va Beach, Va.

Postby leas5040 » Fri Jun 25, 2004 11:26 am

“Beauty is the first test: there is no permanent place in this world for ugly mathematics” (Hardy 1994).


Most likely from Hardy's book A Mathematician's Apology, published in 1941.

Hardy, Ramanujan, Gauss, Riemann, Euler. All were brilliant men and fascinating to read about.
"Given enough time, man can do anything with a bit of string and some Tinker toys." Bruce Bolden, Senior Instructor at the University of Idaho.
User avatar
leas5040
 
Posts: 1214
Joined: Mon Apr 12, 2004 9:51 pm
Location: Moscow, ID

Postby tougo » Fri Jun 25, 2004 11:32 am

Plots points that make a straight line


you already have the answer on how to plot the points that are on circumference of a circle from Alvaro.

Try making a program that plots the points (100*cos(x), 100*sin(x)) for many values of x


to make it more clear

x[i] = startingX+radius*cos(x)
y[i] = startingY+radius*cos(x)

where x are values between 0 to 2*pi so if you need let's say 100 points all you have to do is split the 2*pi into 100 pieces and do a loop 100 times
changing each time the value of x

I feel i didn't reveal too much...i just cleared things that were said
maybe next time that i'll decide to write a task, it would be better to include the mathematics as well
tougo
 
Posts: 432
Joined: Sat Oct 04, 2003 9:18 am
Location: Athens / Greece

Postby RaH » Fri Jun 25, 2004 1:01 pm

No way. It's an excellent task. I just was unsure of the operation to solve for the arc.
I was thinking along the lines of degrees, but I don't think it would apply in this case.

At any rate

Cheers for a great task idea. :D
"Codito ergo sum"
RaH
 
Posts: 258
Joined: Mon May 24, 2004 11:59 am
Location: Va Beach, Va.

Postby RaH » Wed Jun 30, 2004 3:27 pm

At my witts end on this one, nothing I try seems ot work out correctly.
Here is the loop I am having problems with.
Code: Select all
int PlotCircPoints(signed int, signed int, float)
{
      signed int xx, yy;
      yy = y;
      cout << "Origin (" << x << "," << y << endl;
      x = radius + x;

      for(int i = 1; i <= 5; i++)
     {
           cout << "P" <<i << " (" << xx << "," << yy << ")\n";
           xx = ( radius * cos(72 * i)) + x;
           yy = ( radius * sin(72 * i)) + y;
      }
      return x, y;
}

x and y are the users x and y coords.
the output from a radius of 3 with x and y of (2,2) is
--------------------------------------------
Origin (2,2)
P1 (5,2)
P2 (2,2) <------ dead wrong second loop pass
P3 (7,0) <------- also wrong
P4 (2,4) <------ wrong
P5 ( 6,0) <------ way wrong


Notice the lack of negative numbers.
Any ideas of where I went wrong? I have also tried multiple variants of some algorythms I saw posted in this thread, yet I could get none to work as closely as this does.
"Codito ergo sum"
RaH
 
Posts: 258
Joined: Mon May 24, 2004 11:59 am
Location: Va Beach, Va.

Postby MXP » Wed Jun 30, 2004 6:32 pm

sin() and cos() do not use degrees, they use radians. There are 2Pi radians in a circle. Your code can be changed to the following:

Code: Select all
void PlotCircPoints(signed int x, signed int y, float radius)
{
      signed int xx, yy;
      yy = y;
      cout << "Origin (" << x << "," << y << endl;
      xx = radius + x;

      for(int i = 0; i < 5; i++)
     {
           cout << "P" <<i << " (" << xx << "," << yy << ")\n";
           xx = (signed int)(( radius * cos((2 * 3.14159 / 5.0) * i))) + x;
           yy = (signed int)(( radius * sin(72 * i))) + y;
      }
}


*Edit, I also noticed that you are trying to return multiple values - not possible. I changed the function to void since x and y weren't changing anyway.
Need information on a function I've posted? Chances are it's at the MSDN.
MXP
 
Posts: 6506
Joined: Mon Sep 22, 2003 5:27 pm

Postby tougo » Thu Jul 01, 2004 6:17 am

Well I wrote the circlePlot as a seperate program and I thought
to post it

Code: Select all
#include <iostream>
#include <math.h>
#include <tchar.h>

double PI = 3.1415926538;

double PlotPointsx(int lines, double radius, int i)
{
   double theta = ((2* PI)/lines)*i;
   double pointx = radius* sin(theta);
   return pointx;
}
double PlotPointsy(int lines, double radius, int i)
{
   double theta = ((2* PI)/lines)*i;
   double pointy = radius* cos(theta);
   return pointy;
}

int main()
{
   int n;
   std::cout << "insert no of points : ";
   std::cin >> n;
   std::cout << "insert radius : ";
   double radius;
   std::cin >> radius;
  for (int i = 0; i < n; i++)
  {
     std::cout << "point "<< i << ": "<< PlotPointsx(n, radius, i)<< " , " <<  PlotPointsy(n, radius, i) << std::endl;
  }
}




actually this is the first time (as weird as it may seems) that I wrote a console program.

so tell me what you think from the point of structure etc
tougo
 
Posts: 432
Joined: Sat Oct 04, 2003 9:18 am
Location: Athens / Greece

Postby tomcant » Thu Jul 01, 2004 7:01 am

tougo wrote:so tell me what you think from the point of structure etc


You should use <cmath> and main should return a value.
If it wasn't for C, we would be using BASI, PASAL and OBOL.
User avatar
tomcant
 
Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

Postby Alvaro » Thu Jul 01, 2004 7:48 am

tomcant wrote:
tougo wrote:so tell me what you think from the point of structure etc


You should use <cmath> and main should return a value.

main() does return a value. There is an implicit `return 0;'.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Postby tomcant » Thu Jul 01, 2004 8:09 am

Alvaro wrote:
tomcant wrote:
tougo wrote:so tell me what you think from the point of structure etc


You should use <cmath> and main should return a value.

main() does return a value. There is an implicit `return 0;'.


Ok, forget I mentioned it. :wink:
If it wasn't for C, we would be using BASI, PASAL and OBOL.
User avatar
tomcant
 
Posts: 3101
Joined: Tue Sep 23, 2003 1:56 am
Location: Colchester, UK

Postby RaH » Thu Jul 01, 2004 10:27 am

tougo your math is off
ex:
insert no of points : 10
insert radius : 5
point 0: 0 , 5
point 1: 2.93893 , 4.04508
point 2: 4.75528 , 1.54508 <------- note two points above each other
point 3: 4.75528 , -1.54508 <------ below point 2
point 4: 2.93893 , -4.04508
point 5: -1.05103e-09 , -5 <----- no clue what this is

etc etc

I am having the same problems with my algorithms

I think this one has beaten me. :cry: :evil:
"Codito ergo sum"
RaH
 
Posts: 258
Joined: Mon May 24, 2004 11:59 am
Location: Va Beach, Va.

Postby tougo » Thu Jul 01, 2004 11:11 am

RaH wrote:point 2: 4.75528 , 1.54508 <------- note two points above each other
point 3: 4.75528 , -1.54508 <------ below point 2


ofcourse there is a minus in y value that mean that the points are symmetrical to the x axis. also check the following :

RaH wrote:point 1: 2.93893 , 4.04508
point 4: 2.93893 , -4.04508


does it seems familiar?

RaH wrote:point 5: -1.05103e-09 , -5 <----- no clue what this is


Well this is null, zero.... and its actually -1.05103^-09
the value is so small it couldn't be displayed

the program is correct... my math are fine.. but yes i should round my results

I also checked/displayed the results before posting the code
tougo
 
Posts: 432
Joined: Sat Oct 04, 2003 9:18 am
Location: Athens / Greece

Postby tougo » Thu Jul 01, 2004 11:26 am

well i did make a mistake...but the results are still correct. They still form a circle

my mistake was

Code: Select all
double PlotPointsx(int lines, double radius, int i)
{
   double theta = ((2* PI)/lines)*i;
   double pointx = radius* sin(theta); //instead sin i should put cos
   return pointx;
}
double PlotPointsy(int lines, double radius, int i)
{
   double theta = ((2* PI)/lines)*i;
   double pointy = radius* cos(theta); //instead cos i should put sin
   return pointy;
}


it was just upside down. the PlotPointx returns the y values
and the PlotPointy the x values, but still the points form a circle
cause a circle is a symmetric shape, even if you reverse the coordinates you get the same result. Still it was a mistake
tougo
 
Posts: 432
Joined: Sat Oct 04, 2003 9:18 am
Location: Athens / Greece

PreviousNext

Return to Contests

Who is online

Users browsing this forum: No registered users and 2 guests