-
Ellipse problem
Hey,
I need help with my program. I'm really lost and don't know where to start.
Hopefully someone can help me.
1. Use #define to set the value for PI=3.14159265359
2. Consider an ellipse in the x, y-plane. Let a be the semi-major axis
and b be semi-minor axis of the ellipse. The center of the ellipse
is located at (x0, y0). If the major axis coincides with the x-axis,
the polar representation of the ellipse is
x - x0=a cos(theta) and y-y0=b sin(theta) (1)
However, if the major axis of the ellipse is rotated by theta0 from
the x- axis in the counterclockwise direction, the x, y-coordinates
become
x = x0 + a cos(theta)cos(theta0)-b sin(theta)sin(theta0) (2)
y = y0 + a cos(theta)sin(theta0)+b sin(theta)cos(theta0) (3)
Create the void function:
void ellipse(double a, double b, double x0, double y0,
double theta0);
The input arguments are the semi-major axis a, the minor axis b,
the origin of the ellipse (x0, y0), and the tilt angle theta0
(in degrees) of the major axis from the x-axis.
In ellipse() compute and print (x, y) from theta=0 to 360 degrees
with the increment of 10 degrees. To print use %4d, %12.3e, and
%12.3e for theta, x, and y, respectively. Your output in ellipse()
should look like as follows:
-------------------------------
theta x-coord y-coord
-------------------------------
0 3.732e+00 2.000e+00
10 3.619e+00 2.135e+00
... ......... .........
360 3.732e+00 2.000e+00
3. In prob1(), call ellipse() for a=2.0, b=1.0, x0=2.0, y0=1.0,
theta0=30.0.
4. Copy "in_ellipse.dat" from the public directory to your directory
5. In prob2() define the file stream *infile to read "in_ellipse.dat"
6. In prob2() read "in_ellipse.dat" using fgets(). Print each line as
it is read.
7. Finally, print the total number of lines in the file.
-
This is what i have so far
Code:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <math.h>
//use #define for PI
//Function prototypes
void prob1(void);
void prob2(void);
void ellipse(double a, double b, double x0, double y0, double theta0);
int main(void)
{
int menu;
printf("\nThere are two parts.\n");
printf("Enter the part number to execute (1, or 2): ");
scanf("%d", &menu);
// form a switch to execute one part
switch(menu){
case 1:
prob1();
break;
case 2:
prob2();
break;
default:
printf("part %d does not exist.\n", menu);
}
exit(0);
}
void prob1(void)
{
//Declare a, b, x0, y0, and theta0 here
int a, b, x0, y0, theta0;
//Call ellipse here
ellipse()
return;
}
void ellipse(double a, double b, double x0, double y0, double theta0)
{
//Add your statements
return;
}
void prob2(void)
{
//Add your statements
return;
}
-
> from theta=0 to 360 degrees with the increment of 10 degrees.
Your tutor just told you what for loop to write
> To print use %4d, %12.3e, and %12.3e for theta, x, and y, respectively.
Those are the formats you pass to printf()
The formulae for x and y are in the text - read it again.
-
does the for loop goes inside
void ellipse(double a, double b, double x0, double y0, double theta0)??
-
What do you think?
Or more to the point, what does your assignment say you should do. It plainly says where it should go from what I'm reading.
-
This is my program so far i don't get the right values =/
Code:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <math.h>
//use #define for PI
#define PI 3.14159265359
//Function prototypes
void prob1(void);
void prob2(void);
void ellipse(double a, double b, double x0, double y0, double theta0);
int main(void)
{
int menu;
printf("\nThere are two parts.\n");
printf("Enter the part number to execute (1, or 2): ");
scanf("%d", &menu);
// form a switch to execute one part
switch(menu){
case 1:
prob1();
break;
case 2:
prob2();
break;
default:
printf("part %d does not exist.\n", menu);
}
exit(0);
}
void prob1(void)
{
//Declare a, b, x0, y0, and theta0 here
double a=2.0, b=1.0, x0=2.0, y0=1.0, theta0=30.0;
//Call ellipse here
ellipse( a, b, x0, y0, theta0);
return;
}
void ellipse(double a, double b, double x0, double y0, double theta0)
{
//Add your statement
int n, degree;
double x, y, theta;
printf("#-------------------------------\n");
printf("#theta x-coord y-coord\n");
printf("#-------------------------------\n");
for (n=0; n<=36; n++){
degree = n/.1;
theta = degree*(180/PI);
if(theta==0){
x=x0+a*cos(theta);
y=y0+b*sin(theta);
printf("%4d %12.3e %12.3\n",degree, x, y);
}else{
x = x0 + a*cos(theta)*cos(theta0)-b*sin(theta)*sin(theta0);
y = y0 + a*cos(theta)*sin(theta0)+b*sin(theta)*cos(theta0);
printf(" %4d %12.3e %12.3e\n", degree, x, y);
}}
return;
}
void prob2(void)
{
//Add your statements
FILE *infile;
char text [75];
int count =0;
printf("\n");
infile = fopen("in_ellipse.dat", "r");
if (infile==NULL){
printf("The input file does not exist.\n");
exit(0);
}else
while(fgets(text,75,infile)!=NULL)
{
count++;
printf("%s",text);
}
printf("\n There are %d lines.\n",count);
fclose(infile);
return;
}
-
my problem is that theta is being read as radians instead of degrees.. can anyone help me
-
your loop
Did you figure it out? Im curious about that degree change too.
-
I am getting super big numbers too. Did you get it?
-
I tried it, I got big numbers also and it almost looks as if the loop isnt running properly.
-
damn, i still cant get it
-
So, what exactly do you have problems with?
-
it should be
theta = degree* (PI/180.0)
-
you also have to do
theta0 = 30.0*(PI/180.0)
-
.
The question was how to convert radians to degrees in order to run it in the for loop so that the intervals would be increasing at 10 degrees each time.