In: Computer Science
Write the following functions using the following structure for a point.
struct Point {
double x, y;
};
(a) A function that passes a point and returns an integer (1, 2, 3, or 4) to indicate in which quadrant the point is located
int findQuadrant(struct Point testpoint)
{
}
(b) A function that passes two points and returns the distance between them.
float distance(struct Point point1, struct Point point2)
{
}
(c) A function that passes two points and generate the line equation that passes these two points. The function should return the slope and y-intercept.
void line(struct Point point1, struct Point point2, float *slope, float *intercept)
{
}
Thanks for the question.
Here is the completed code for this problem. Comments are
included so that you understand whats going on. Let me know if you
have any doubts or if you need anything to change.
Thank You !!
========================================================================================
#include <iostream>
#include<cmath>
using namespace std;
struct Point {
double x, y;
};
int findQuadrant(struct Point testpoint){
if(testpoint.x>=0){
if(testpoint.y>=0){
return 1 // when
both x and y are positive
}else{
return 3; //
when x is positive and y is negative
}
}else{4
if(testpoint.y>=0){
return 2 // when
x is negative and y is positive
}else{
return 3; //
when x is positive and y is negative
}
}
}
float distance(struct Point point1, struct Point
point2){
float squareX =
(point1.x-point2.x)*(point1.x-point2.x);
float squareY =
(point1.y-point2.y)*(point1.y-point2.y);
return pow(squareX+squareY,0.5); // square root of x^2
+ y^2
}
void line(struct Point point1, struct Point point2, float
*slope, float *intercept){
if(point1.x==point2.x){
*intercept=0; // there will be no y
intercept
*slope=0; // slope will be
infinity
}else if(point1.y==point2.y){
*intercept=point1.y; // intercept
will be that of point1.y or point2.y
*slope=0; // slope will be
zero
}else{
*slope=(point1.y-point2.y)/(point1.x-point2.x); // find slope
*intercept=*slope*point1.x
+point1.y; // use slope and any other point to find the
intercept
}
}
========================================================================================