Question

please answer the second part first part answered. the answer to the first part is at...

please answer the second part first part answered.
the answer to the first part is at the bottom
bring the rest of the place
HAVE TO BE SOLVED WİTH C PROGRAMMİNG


A programming task is given in below (i.e., you are supposed to write a single program involving all 5 tasks in below). You should write a C program and upload your c/cpp file .
Students are allowed to use MAK104E course (powerpoint) slights.


1) Three geometric entities (a circle, a rectangle and a triangle) should be first randomized using rand function in line with srand(time(NULL)).
These three geometrric entities can/should be represented using the following C struct samples in
struct Circle

{

double x;

double y;

double r; };
struct rectangle

{

double xBottomLeft;

double yBottomLeft;

double xtopRight;

double ytopLeft; };
struct Triangle

{

double x1;

double y1;

double x2;

double y2;

double x3;

double y3; };
these three geometrric entities can/should be represented using the following C struct samples in your program:


Write three functions for each geometric entity that randomizes the geometric parameters (such as center X and Y coordinates and radius for a circle) and returns these geometries (such as struct Circle, struct Rectangle and struct Triangle). Note that floating numbers should be randmized from 0 to 10.
  
2) Write three functions for each geometric entity that samples 300 points on these geometries and returns these points. Note that these points should be represented using a linked list as in below (in such a way that Pt1
--> Pt2 --> .... --> Pt300 --> NULL).
Use pointers to represent these linked lists. Do not forget allocate memory (new) for pointers and deallocate (free) after using them.
struct point
{
double x;
double y;
struct point *pnext;
};

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

struct Circle

{

double x;

double y;

double r;

  

};

struct Circle randomize_circle()

{

struct Circle c1;

double a=10;

/*here a=10 is the range from 0 to 10 upt0 which we

want to get the double values. as defined here.

since we cant get the direct random double values so

we find random int values in range of 0 to RAND_MAX

and then we divide it by RAND_MAX to get it in range of

[0 to 1] and then we multiply it with 10 to get the

to get it in range of [0 10]..

*/

c1.x= (double)(rand()*a)/(double)(RAND_MAX);

c1.y=(double)(rand()*a)/(double)(RAND_MAX);

c1.r=(double)(rand()*a)/(double)(RAND_MAX);

//assingn the random x ,y and return c1..

return c1;

  

}

//we have the rectangle as x y and x top and y top here as defined

struct rectangle

{

double xBottomLeft;

double yBottomLeft;

double xtopRight;

double ytopLeft;

  

};

struct rectangle randomize_rectangle()

{

struct rectangle r1;

double a=10.00;

r1.xBottomLeft=(double)(rand()*a)/(double)(RAND_MAX);

r1.yBottomLeft=(double)(rand()*a)/(double)(RAND_MAX);

r1.xtopRight=(double)(rand()*a)/(double)(RAND_MAX);

r1.ytopLeft=(double)(rand()*a)/(double)(RAND_MAX);

return r1;

  

}

struct Triangle

{

double x1;

double y1;

double x2;

double y2;

double x3;

double y3;

  

};

//we get the randomize_triangle and randomize_rectangle as defined...

struct Triangle randomize_triangle()

{

struct Triangle t1;

double a=10.00;

t1.x1=(double)(rand()*a)/(double)(RAND_MAX);

t1.y1=(double)(rand()*a)/(double)(RAND_MAX);

t1.x2=(double)(rand()*a)/(double)(RAND_MAX);

t1.y2=(double)(rand()*a)/(double)(RAND_MAX);

t1.x3=(double)(rand()*a)/(double)(RAND_MAX);

t1.y3=(double)(rand()*a)/(double)(RAND_MAX);

return t1;

  

}

int main(){

  

srand((unsigned int )time(NULL));

  

//we randomize_circle the teh rectangle and Triangle and get the rectangle

struct Circle c2= randomize_circle();

struct rectangle r2= randomize_rectangle();

struct Triangle t1=randomize_triangle();

  

  

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the solution to your question. I tried my best to solve your doubt, however, if you find it is not as good as expected by you. Please do write your further doubts regarding this question in the comment section, I will try to resolve your doubts regarding the submitted solution as soon as possible.

Please give proper indentation as shown in the screenshot

If you think, the solution provided by me is helpful to you please do an upvote.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>

struct point
{
double x;
double y;
struct point *pnext;
};

double getY(double x,double slope,double x1,double y1)
{
return slope*x-slope*x1+y1;
}

struct Circle
{
double x;
double y;
double r;
  
};

struct Circle randomize_circle()
{
struct Circle c1;
c1.x=(rand() % (10 - 1 + 1)) + 1;
  
c1.y=(rand() % (10 - 1 + 1)) + 1;
c1.r=(rand() % (10 - 1 + 1)) + 1;
  
return c1;
  
}

struct point* Circle300(struct Circle C)
{
struct point* pts=(struct point*)malloc(sizeof(struct point));
struct point* temp=(struct point*)malloc(sizeof(struct point));
temp->x=C.x;
temp->y=C.y;
temp->pnext=NULL;
pts=temp;
int cnt=0;
struct point* ptemp=pts;
for(double i=0.0;i<=300.0;i+=1)
{
cnt+=1;
double angle = i; //in degrees
double rad=( angle * 3.1415 / 180 ); //converting to rads
struct point* ntemp=(struct point*)malloc(sizeof(struct point));
ntemp->x=C.r*cos(rad);
ntemp->y=C.r*sin(rad);
ntemp->pnext=NULL;
ptemp->pnext=ntemp;
ptemp=ptemp->pnext;
}


return pts;
}

struct rectangle
{
double xBottomLeft;
double yBottomLeft;
double xtopRight;
double ytopRight;
  
};

struct rectangle randomize_rectangle()
{
struct rectangle r1;
r1.xBottomLeft=(rand() % (10 - 1 + 1)) + 1;
r1.yBottomLeft=(rand() % (10 - 1 + 1)) + 1;
r1.xtopRight=(rand() % (10 - 1 + 1)) + 1;
r1.ytopRight=(rand() % (10 - 1 + 1)) + 1;
return r1;
  
}

struct point* Rectangle300(struct rectangle R)
{
double slope;
struct point* pts=(struct point*)malloc(sizeof(struct point));
struct point* temp=(struct point*)malloc(sizeof(struct point));
temp->x=R.xBottomLeft;
temp->y=R.yBottomLeft;
temp->pnext=NULL;
pts=temp;
struct point* ptemp=pts;
double x1;
double y1;
double dif;
double x;
int cnt=0;

x1=R.xBottomLeft;
y1=R.yBottomLeft;
slope=0;
dif=(R.xtopRight-R.xBottomLeft)/75;
x=x1;
for(int i=0;i<75;i+=1)
{
cnt+=1;
struct point* ntemp=(struct point*)malloc(sizeof(struct point));
x+=dif;
ntemp->x=x;
ntemp->y=getY(ntemp->x, slope, x1, y1);
//printf("%d %lf %lf %lf\n",cnt,ntemp->x,ntemp->y,dif);
ntemp->pnext=NULL;
ptemp->pnext=ntemp;
ptemp=ptemp->pnext;
}

x1=R.xtopRight;
y1=R.yBottomLeft;
//slope=(R.ytopRight-R.yBottomLeft)/(T.x3-T.x2);
dif=(R.ytopRight-R.yBottomLeft)/75;
x=y1;
for(int i=0;i<75;i+=1)
{
cnt+=1;
struct point* ntemp=(struct point*)malloc(sizeof(struct point));
ntemp->x=R.xtopRight;
ntemp->y=x;
x+=dif;
//printf("%d %lf %lf\n",cnt,ntemp->x,ntemp->y);
ntemp->pnext=NULL;
ptemp->pnext=ntemp;
ptemp=ptemp->pnext;
}

x1=R.xtopRight;
y1=R.ytopRight;
slope=0;;
dif=(R.xBottomLeft-R.xtopRight)/75;
x=x1;
for(int i=0;i<75;i+=1)
{
cnt+=1;
struct point* ntemp=(struct point*)malloc(sizeof(struct point));
ntemp->x=x;
x+=dif;
ntemp->y=getY(ntemp->x, slope, x1, y1);
//printf("%d %lf %lf\n",cnt,ntemp->x,ntemp->y);
ntemp->pnext=NULL;
ptemp->pnext=ntemp;
ptemp=ptemp->pnext;
}

x1=R.xBottomLeft;
y1=R.ytopRight;
//slope=(R.ytopRight-R.yBottomLeft)/(T.x3-T.x2);
dif=(R.yBottomLeft-R.ytopRight)/75;
x=y1;
for(int i=0;i<75;i+=1)
{
cnt+=1;
struct point* ntemp=(struct point*)malloc(sizeof(struct point));
ntemp->x=R.xBottomLeft;
ntemp->y=x;
x+=dif;
//printf("%d %lf %lf\n",cnt,ntemp->x,ntemp->y);
ntemp->pnext=NULL;
ptemp->pnext=ntemp;
ptemp=ptemp->pnext;
}

return pts;
}

struct Triangle
{
double x1;
double y1;
double x2;
double y2;
double x3;
double y3;
  
};

struct Triangle randomize_triangle()
{
struct Triangle t1;
t1.x1=(rand() % (10 - 1 + 1)) + 1;
t1.y1=(rand() % (10 - 1 + 1)) + 1;
t1.x2=(rand() % (10 - 1 + 1)) + 1;
t1.y2=(rand() % (10 - 1 + 1)) + 1;
t1.x3=(rand() % (10 - 1 + 1)) + 1;
t1.y3=(rand() % (10 - 1 + 1)) + 1;
return t1;
  
}

struct point* Triangle300(struct Triangle T)
{
double slope;
struct point* pts=(struct point*)malloc(sizeof(struct point));
struct point* temp=(struct point*)malloc(sizeof(struct point));
temp->x=T.x1;
temp->y=T.y1;
temp->pnext=NULL;
pts=temp;
struct point* ptemp=pts;
double x1;
double y1;
double dif;
double x;
int cnt=0;

x1=T.x1;
y1=T.y1;
  
  
dif=(T.x2-T.x1)/100;
if(dif!=0)
slope=(T.y2-T.y1)/(T.x2-T.x1);
else
slope=0;
x=x1;
for(int i=0;i<100;i+=1)
{
cnt+=1;
struct point* ntemp=(struct point*)malloc(sizeof(struct point));
if(dif!=0)
{
x+=dif;
ntemp->x=x;
ntemp->y=getY(ntemp->x, slope, x1, y1);
}
else
{
ntemp->x=x;
ntemp->y+=(T.y2-T.y1)/100;

}
//printf("%d %lf %lf\n",cnt,ntemp->x,ntemp->y);
ntemp->pnext=NULL;
ptemp->pnext=ntemp;
ptemp=ptemp->pnext;
}

x1=T.x2;
y1=T.y2;
  
dif=(T.x3-T.x2)/100;
if(dif!=0)
slope=(T.y3-T.y2)/(T.x3-T.x2);
else
slope=0;
x=x1;
for(int i=0;i<100;i+=1)
{
cnt+=1;
struct point* ntemp=(struct point*)malloc(sizeof(struct point));
if(dif!=0)
{
ntemp->x=x;
x+=dif;
ntemp->y=getY(ntemp->x, slope, x1, y1);
}
else
{
ntemp->x=x;
ntemp->y+=(T.y3-T.y2)/100;

}

//printf("%d %lf %lf\n",cnt,ntemp->x,ntemp->y);
ntemp->pnext=NULL;
ptemp->pnext=ntemp;
ptemp=ptemp->pnext;
}

x1=T.x3;
y1=T.y3;
  
dif=(T.x1-T.x3)/100;
if(dif!=0)
{
slope=(T.y1-T.y3)/(T.x1-T.x3);
}
else
slope=0;
x=x1;
for(int i=0;i<100;i+=1)
{
cnt+=1;
struct point* ntemp=(struct point*)malloc(sizeof(struct point));
if(dif!=0)
{
ntemp->x=x;
x+=dif;
ntemp->y=getY(ntemp->x, slope, x1, y1);
}
else
{
ntemp->x=x;
ntemp->y+=(T.y1-T.y3)/100;
}
//printf("%d %lf %lf\n",cnt,ntemp->x,ntemp->y);
ntemp->pnext=NULL;
ptemp->pnext=ntemp;
ptemp=ptemp->pnext;
}
return pts;
}

int main()
{
  
srand(time(0));
  
struct Circle c2= randomize_circle();
struct rectangle r2= randomize_rectangle();
struct Triangle t1=randomize_triangle();
struct point* ptsCircle=Circle300(c2);
struct point* ptsRectangle=Rectangle300(r2);
struct point* ptsTriangle=Triangle300(t1);

struct point* tptr;

tptr=ptsCircle;
printf("Circle points are:\n");
for(int i=0;i<300;i+=1)
{
printf("%d (%lf, %lf)\n",(i+1),tptr->x,tptr->y);
tptr=tptr->pnext;
}

tptr=ptsRectangle;
printf("Rectangle points are:\n");
for(int i=0;i<300;i+=1)
{
printf("%d (%lf, %lf)\n",(i+1),tptr->x,tptr->y);
tptr=tptr->pnext;
}

tptr=ptsTriangle;
printf("Triangle points are:\n");
for(int i=0;i<300;i+=1)
{
printf("%d (%lf, %lf)\n",(i+1),tptr->x,tptr->y);
tptr=tptr->pnext;
}

  
  
return 0;


}

Add a comment
Know the answer?
Add Answer to:
please answer the second part first part answered. the answer to the first part is at...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Three geometric entities (a circle, a rectangle and a triangle) should be first randomized using rand...

    Three geometric entities (a circle, a rectangle and a triangle) should be first randomized using rand function in line with srand(time(NULL)). These three geometrric entities can/should be represented using the following C struct samples in your program: Write three functions for each geometric entity that randomizes the geometric parameters (such as center X and Y coordinates and radius for a circle) and returns these geometries (such as struct Circle, struct Rectangle and struct Triangle). Note that floating numbers should be...

  • Create a UML diagram with 3 lines per class/interface including all constructors. public class Point {...

    Create a UML diagram with 3 lines per class/interface including all constructors. public class Point { public double X, Y; public Point() { this(0, 0); } public Point(double newX, double newY) { X = newX; Y = newY; } public static double distance(Point A, Point B) { return Math.sqrt(Math.pow(A.X-B.X, 2) + Math.pow(A.Y-B.Y, 2)); } } public interface Polygon { public int getNumberOfSides();    public double getPerimeter();    public double getArea();    } public abstract class Simple_polygon implements Polygon{ public Point...

  • 1. In IntelliJ create a new project called F1_2 2. In the Project window create a...

    1. In IntelliJ create a new project called F1_2 2. In the Project window create a new Java package called F1_2. This can be done by right clicking on src and going to New ! Package. 3. In package lab1 2 create a class called Rectangle. This can be done by right clicking on the package and going to New ! Java Class 4. At the beginning of Rectangle.java add the line package lab1 2; 5. In Rectangle.java create a...

  • GUI in java: Run the below code before doing the actionlistener: import java.awt.*; import javax.swing.*; public...

    GUI in java: Run the below code before doing the actionlistener: import java.awt.*; import javax.swing.*; public class DrawingFrame extends JFrame {    JButton loadButton, saveButton, drawButton;    JComboBox colorList, shapesList;    JTextField parametersTextField;       DrawingFrame() {        super("Drawing Application");        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        JToolBar toolbar = new JToolBar();        toolbar.setRollover(true);        toolbar.add(loadButton=new JButton("Load"));        toolbar.add(saveButton=new JButton("Save"));        toolbar.addSeparator();        toolbar.add(drawButton=new JButton("Draw"));               toolbar.addSeparator();        toolbar.addSeparator();        toolbar.add(new...

  • JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST...

    JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST CASES(WHAT IM TRYING TO PRODUCE) BELOW THOSE IMAGES ARE THE .JAVA FILES THAT I HAVE CREATED. THESE ARE GeometircObject.Java,Point.java, and Tester.Java. I just need help making the Rectangle.java and Rectangle2D.java classes. GeometricObject.Java: public abstract class GeometricObject { private String color = "white"; // shape color private boolean filled; // fill status protected GeometricObject() { // POST: default shape is unfilled blue this.color = "blue";...

  • Please help with C Thanks Declare a structure to represent a circle. Each circle stores information...

    Please help with C Thanks Declare a structure to represent a circle. Each circle stores information about the center and the radius. Center is represented with x and y coordinates. Both center and radius uses doubles. Allocate an array of 50 circle pointers and dynamically allocate memory to store a circle pointed by cash array eatery. Randomly generate circles using the following function. double rand_float (double a.double b) {return ((double)rand()/RAND_MAX)=(b-a)+a:} We want all the circles to fit an area of...

  • Using python Here is the code import turtle def draw_triangle(vertex, length, k): ''' Draw a ...

    Using python Here is the code import turtle def draw_triangle(vertex, length, k): ''' Draw a triangle at depth k given the bottom vertex.    vertex: a tuple (x,y) that gives the coordinates of the bottom vertex. When k=0, vertex is the left bottom vertex for the outside triangle. length: the length of the original outside triangle (the biggest one). k: the depth of the input triangle. As k increases by 1, the triangles shrink to a smaller size. When k=0, the...

  • I need help with C, please don't copy other answers!! Thanks Declare a structure to represent...

    I need help with C, please don't copy other answers!! Thanks Declare a structure to represent a circle. Each circle stores information about the center and the radius. Center is represents with x and y coordinates. Both center and radius uses doubles. Allocate an array of 502 circle pointers and dynamically allocated memory to store a circle pointed by each array entry. Randomly generate circles using the following function. double rand_ float (double a, double b) {return ((double) rand()/RAND_MAX) *...

  • MATLAB only please I am trying to get my getdatafuntion to work. I am also trying to get all my x...

    MATLAB only please I am trying to get my getdatafuntion to work. I am also trying to get all my x's, y's, and v's to popup in the command window so I can put in any value and it will find the value for me using the equation I put in. function project_9_sjl() % PROJECT_9_SJL project_9_sjl() is the driver function for the program. % %    Name: Scott Lawrence %   Date: 3/27/2019 %   Class: CMPSC 200 %   Description: Determine the optimal...

  • Please use C++ and Please do all file separate separately. And also I need output too....

    Please use C++ and Please do all file separate separately. And also I need output too. thanks. Please do it complete work. ShapeNodePoly.cpp Avaliable from: Wednesday, July 27, 2016, 10:20 AM Requested files: Point.h, Point.cpp, Shape.h, Shape.cpp, Polygon.h, Polygon.cpp, Ellipse.h, Ellipse.cpp, ShapeNodePoly.cpp, ShapeNodePoly_test.cpp (Download) Type of work: Individual work In this assignment, you will create a class that can behave as any of the shapes listed below. You will use object inheritance to enable all shapes to be managed using...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT