Teacher’s Notes:
=======================================
This assignment could also be reworked to include the use of functions/methods in the calculations.
This is a good assignment for getting students to do things like formatting output, and setting up complex mathematical equations.
Assignment:
=======================================
Coordinate Geometry
In coordinate geometry, a line segment can be defined by giving two endpoints in Cartesian form (x1,y1) and (x2,y2).
Part 1:
Write a program that will prompt the user to enter the coordinates x1, y1, x2, and y2. These should be read as a decimal value. It is not required to error trap this input.
Part 2: Slope
The slope of this line segment can be found using the equation (x2-x1) / (y2 – y1). This represents the rate of vertical change vs. the rate of horizontal change. Note that if (x2-x1) is equal to zero, the line is a vertical line and the slope is considered to be undefined. Expand on the program written in part 1 to output the slope of the line.
Part 3: Length
We can find the vertical distance from endpoint to endpoint by subtracting (y2-y1). We can find the horizontal distance by subtracting (x2-x1). Once we have these values, we can use the Pythagorean theorem to find the length of the line segment such that:
Expand the program to also output the length of the line segment.
Part 4: Mid-Point
The midpoint of the line segment can be found by finding the average of the two x values and the average of the two y values such that:
Expand the program to also output the midpoint of the line segment.
Example 1:
Enter the value of x1: 0
Enter the value of y1: 0
Enter the value of x2: 0
Enter the value of y2: 5
Line Information:
=================
Slope: undefined
Length: 5.0
Mid-Point:(0.0,2.5)
Example 2:
Enter the value of x1: 2
Enter the value of y1: 5
Enter the value of x2: 6
Enter the value of y2: -7
Line Information:
=================
Slope: -3.0
Length: 12.649110640673518
Mid-Point:0(4.0,-1.0)
Solution (JAVA):
=======================================
import java.util.Scanner;
public class CoordinateGeometry {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the value of x1: ");
double x1 = input.nextDouble();
System.out.print("Enter the value of y1: ");
double y1 = input.nextDouble();
System.out.print("Enter the value of x2: ");
double x2 = input.nextDouble();
System.out.print("Enter the value of y2: ");
double y2 = input.nextDouble();
System.out.println("\nLine Information:");
System.out.println("=================");
if(x2-x1 != 0)
System.out.println("Slope:\t" + (y2-y1)/(x2-x1));
else
System.out.println("Slope:\tundefined");
System.out.println("Length:\t" + Math.sqrt(Math.pow((x2-x1),2) + Math.pow((y2-y1),2)));
System.out.println("Mid-Point:0"
+ "(" + (x2+x1)/2 + "," + (y2+y1)/2 + ")");
}
}
Solution (C++):
=======================================
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double x1, y1, x2, y2;
cout << "Enter the value of x1: ";
cin >> x1;
cout << "Enter the value of y1: ";
cin >> y1;
cout << "Enter the value of x2: ";
cin >> x2;
cout << "Enter the value of y2: ";
cin >> y2;
cout << "\nLine Information:" << endl;
cout << "=================" << endl;
if(x2-x1 != 0)
cout << "Slope:\t" << (y2-y1)/(x2-x1) << endl;
else
cout << "Slope:\tundefined" << endl;
cout << "Length:\t" << sqrt(pow((x2-x1),2) + pow((y2-y1),2)) << endl;
cout << "Mid-Point:\t(" << (x2+x1)/2 << "," << (y2+y1)/2 << ")" << endl;
return 0;
}