Skip to content

Instantly share code, notes, and snippets.

@pirogoeth
Created October 30, 2012 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pirogoeth/3977891 to your computer and use it in GitHub Desktop.
Save pirogoeth/3977891 to your computer and use it in GitHub Desktop.
A little program I wrote to do my precal homework problems for me.
import java.lang.Math;
import java.util.*;
public class Lines {
public CoordSet first;
public CoordSet second;
public static class CoordSet {
public double x, y, h, k;
public CoordSet(double x, double y, double h, double k) {
this.x = x;
this.y = y;
this.h = h;
this.k = k;
}
public CoordSet(String x, String y, String h, String k) {
this.x = Double.valueOf(x);
this.y = Double.valueOf(y);
this.h = Double.valueOf(h);
this.k = Double.valueOf(k);
}
public String toString() {
return String.format("(%f, %f), (%f, %f)", this.x, this.y, this.h, this.k);
}
public double getSlope() {
return ((h - x) / (k - y));
}
public double getReciprocalSlope() {
return (-1.0D / this.getSlope());
}
}
public Lines(CoordSet first, CoordSet second) {
this.first = first;
this.second = second;
}
public static void main(String[] args) {
String s1 = args[0].split("\\|")[0];
String s2 = args[0].split("\\|")[1];
String[] a1 = s1.split("\\,");
String[] a2 = s2.split("\\,");
Lines lineSet = new Lines(new CoordSet(a1[0], a1[1], a1[2], a1[3]), new CoordSet(a2[0], a2[1], a2[2], a2[3]));
System.out.println("Line 1: " + lineSet.first.toString());
System.out.println(" => Slope: " + lineSet.first.getSlope() + ", " + lineSet.first.getReciprocalSlope());
System.out.println("Line 2: " + lineSet.second.toString());
System.out.println(" => Slope: " + lineSet.second.getSlope() + ", " + lineSet.second.getReciprocalSlope());
// check for parallelness
if (lineSet.first.getSlope() == lineSet.second.getSlope()) {
System.out.println("Lines 1 and 2 are parallel.");
} else {
System.out.println("Lines 1 and 2 are not parallel.");
}
// check for perpendicularness
if (lineSet.first.getSlope() == lineSet.second.getReciprocalSlope() && lineSet.first.getReciprocalSlope() == lineSet.second.getSlope()) {
System.out.println("Lines 1 and 2 are perpendicular.");
} else {
System.out.println("Lines 1 and 2 are not perpendicular.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment