In: Computer Science
C++
Create a program which randomly generates 3 sets of x and y Integers and one randomly generated Integer. Two of the sets of Integers will represent the end points of a line segment. The other set of Integers and the other Integer will represent the midpoint of a circle and its radius.
The coordinates should be randomly generated using a user defined function that returns an Integer value based on from and to parameters; see the function declaration in the Other section below. Your coordinates should be randomly selected from -99 to 99. The radius should be a randomly generated number (using the same function) from 1 to 200.
Two other functions should be created to determine if a line segment is wholly within a circle. One of the functions should return the length of a line segment and the other will return a Boolean indicating if the passed line segment is in the passed circle; again, see the function declarations below.
The program should display all of the generated data and one of the messages regarding the location of the line as shown below in Output Layout section. Try to make everything line up correctly.
Output Layout:
Coordinates of a Random Line Segment 1st Point's x coordinate: -## 1st Point's y coordinate: -## 2nd Point's x coordinate: -## 2nd Point's y coordinate: -## Coordinates of a Random Circle coordinate: -## coordinate: -## Radius: ### The line segment is within the circle. OR The line segment is not within the circle.
Other: (Required Identifier Names, Prototypes & Random Ranges)
p0x // an int from -99 to 99 p0y // an int from -99 to 99 p1x // an int from -99 to 99 p1y // an int from -99 to 99 mpx // an int from -99 to 99 mpy // an int from -99 to 99 radius // an int from 1 to 200 int randomInteger(const int from, const int to); int lineSegLength(const int p0x, const int p0y, const int p1x, const int p1y); bool lineInCircle(const int p0x, const int p0y, const int p1x, const int p1y, const int mpx, const int mpy, const int radius);
THANKS FOR THE QUESTION. HERE IS THE COMPLETE CODE IN C++
=====================================================================================
#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
int randomInteger(const int from, const int to){
return from + rand()%(to-from-1);
}
int lineSegLength(const int p0x, const int p0y, const int p1x, const int p1y){
int squared_sum = (p0x-p1x)*(p0x-p1x) + (p0y-p1y)*(p0y-p1y);
return static_cast<int>(pow(squared_sum,0.5));
}
bool lineInCircle(const int p0x, const int p0y, const int p1x, const int p1y, const int mpx, const int mpy, int radius){
int end_point0_distance = lineSegLength(p0x,p0y,mpx,mpy);
if(end_point0_distance>radius)return false;
int end_point1_distance = lineSegLength(p1x,p1y,mpx,mpy);
return end_point1_distance<=radius;
}
int main(){
int x1,y1;
int x2,y2;
int midpoint_x, midpoint_y; int radius;
x1=randomInteger(-99,99);
y1=randomInteger(-99,99);
x2=randomInteger(-99,99);
y2=randomInteger(-99,99);
midpoint_x=randomInteger(-99,99);
midpoint_y=randomInteger(-99,99);
radius = randomInteger(1,200);
cout<<"1st Point\'s x coordinate: "<<x1<<endl;
cout<<"1st Point\'s y coordinate: "<<y1<<endl;
cout<<"2nd Point\'s x coordinate: "<<x2<<endl;
cout<<"2nd Point\'s y coordinate: "<<y2<<endl;
cout<<endl;
cout<<"Coordinates of a Random Circle\n";
cout<<"coordinate: "<<midpoint_x<<endl;
cout<<"coordinate: "<<midpoint_y<<endl;
cout<<"Radius: "<<radius<<endl<<endl;
cout<<endl;
if(lineInCircle(x1,y1,x2,y2,midpoint_x,midpoint_y,radius)){
cout<<"The line segment is within the circle.";
}else{
cout<<"The line segment is not within the circle.";
}
}
=============================================================================