java - Implementing classes, Triangle isosceles -
i need implement triangle class , im stuck on comparing lengths of sides determine if triangle indeed isosceles. here have far:
public class triangleisosceles { private point cornera; private point cornerb; private point cornerc; private int x1; private int y1; private int x2; private int y2; private int x3; private int y3; public triangleisosceles(){ cornera = new point(0,0); cornerb = new point(10,0); cornerc = new point(5,5); } public triangleisosceles(int x1,int y1,int x2,int y2,int x3,int y3){ cornera = new point(x1,y1); cornerb = new point(x2,y2); cornerc = new point(x3,y3); } public string isisosceles(string isisosceles){ homecoming isisosceles; } }
the point
object im using this:
public class point { private int x; private int y; public point(){ this(0,0); } public point(int x, int y){ this.x = x; this.y = y; } public void setx(int x){ this.x=x; } public void sety(int y){ this.y=y; } public void printpoint(){ system.out.println(x + y); } public string tostring(){ homecoming "x = "+x+" y = "+y; } }
in class (linesegment
) created method length()
determines distance of 2 points. looks like:
public double length() { double length = math.sqrt(math.pow(x1-x2,2) + math.pow(y1-y2,2)); homecoming length; }
how can utilize method help me find lengths of triangle in triangleisosceles
class?
i know need see if (lenghtab == lengthbc || lengthbc == lenghtca || lengthab == lengthca)
.
a quick, valid, solution create length method static utility method, i.e.
public static double length(x1, y1, x2, y2) { homecoming math.sqrt(math.pow(x1 - x2, 2) + math.pow(y1 - y2, 2)); } or public static double length(point p1, point p2) { homecoming math.sqrt(math.pow(p1.x - p2.x, 2) + math.pow(p1.y - p2.y, 2)); }
you add together method point itself, i.e. in point class add:
public double calcdistance(point otherpoint) { homecoming math.sqrt(math.pow(this.x - otherpoint.x, 2) + math.pow(this.y - otherpoint.y, 2)); }
java geometry implementation
No comments:
Post a Comment