Reputation:
This is my current program that finds a perimeter of a rectangle given four points. I am doing it for a school project and want to make sure that the lines form a valid rectangle but I don't even know where to start to check for intersecting lines here. How would I do that?
package perimeter;
import java.util.Scanner;
import javafx.geometry.Point2D;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter point 1's x-, y-coordinates: ");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
System.out.print("Enter point 2's x-, y-coordinates: ");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
System.out.print("Enter point 3's x-, y-coordinates: ");
double x3 = input.nextDouble();
double y3 = input.nextDouble();
System.out.print("Enter point 4's x-, y-coordinates: ");
double x4 = input.nextDouble();
double y4 = input.nextDouble();
input.close();
Point2D p1 = new Point2D(x1, y1);
Point2D p2 = new Point2D(x2, y2);
Point2D p3 = new Point2D(x3, y3);
Point2D p4 = new Point2D(x4, y4);
double Perimeter = p1.distance(p2) + p2.distance(p3) +
+ p3.distance(p4) + p4.distance(p1);
System.out.println("The perimeter is " +
Perimeter);
}
}
Upvotes: 1
Views: 1016
Reputation: 361
If you have a rectangle a, b, c, d, check if the distance between a and c is equal to the distance between b and d. That will verify that the shape is a rectangle
double distance = Math.hypot(x1-x2, y1-y2);
Read the docs here: https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#hypot%28double,%20double%29
Upvotes: 0
Reputation: 21
To get perimeter you have to calculate total distance around rectangle, but I think your question is how to check if it is really rectangle given 4 points. I think best way would be checking if 3 points make a right angle(which 3 point would depend on order of your 4 points) Could help little more if we see some code.
Upvotes: 1