daily pastebin goal
28%
SHARE
TWEET

Untitled

a guest Jan 17th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Triangle {
  2.     private double ax;
  3.     private double ay;
  4.     private double bx;
  5.     private double by;
  6.     private double cx;
  7.     private double cy;
  8.     //added these variables because I use them so frequently when calculating angles, area, perimeter, etc.
  9.     private double sideAB;
  10.     private double sideBC;
  11.     private double sideAC;
  12.  
  13.     public Triangle(double x1, double y1, double x2, double y2, double x3, double y3)
  14.     {
  15.         ax = x1;
  16.         ay = y1;
  17.         bx = x2;
  18.         by = y2;
  19.         cx = x3;
  20.         cy = y3;
  21.         sideAB= Math.abs(Math.sqrt(Math.pow(bx-ax,  2)+Math.pow(by-ay, 2)));
  22.         sideBC= Math.abs(Math.sqrt(Math.pow(cx-bx,  2)+Math.pow(cy-by, 2)));
  23.         sideAC= Math.abs(Math.sqrt(Math.pow(cx-ax,  2)+Math.pow(cy-ay, 2)));
  24.     }
  25.     public double getPerimeter()
  26.     {
  27.         //add the 3 sides together for the perimeter
  28.         double perimeter = sideAB + sideBC + sideAC;
  29.         return perimeter;
  30.     }
  31.     public double getArea()
  32.     {
  33.         //used Heron's formula to find the area of the triangle
  34.         double s = (sideAB + sideBC + sideAC)/2;
  35.         double area = Math.sqrt(s*(s - sideAB)*(s - sideBC)*(s - sideAC));
  36.         return area;
  37.     }
  38.     public double getSideAB()
  39.     {
  40.         return sideAB;
  41.     }
  42.     public double getSideBC()
  43.     {
  44.         return sideBC;
  45.     }
  46.     public double getSideAC()
  47.     {
  48.         return sideAC;
  49.     }
  50.     public double getAngleC()
  51.     {
  52.         //Law of cosines to find the angle
  53.         double a2 = Math.pow(sideAB, 2);
  54.         double b2 = Math.pow(sideBC, 2);
  55.         double c2 = Math.pow(sideAC, 2);
  56.         double cosC = ((b2 + c2)-a2)/((2*sideBC)*sideAC);
  57.         double angleC = Math.acos(cosC);
  58.         angleC = Math.toDegrees(angleC);
  59.         return angleC;
  60.     }
  61.     public double getAngleB()
  62.     {
  63.         double a2 = Math.pow(sideAB, 2);
  64.         double b2 = Math.pow(sideBC, 2);
  65.         double c2 = Math.pow(sideAC, 2);
  66.         double cosB = ((a2+b2-c2)/(2*sideAB*sideBC));
  67.         double angleB = Math.acos(cosB);
  68.         angleB = Math.toDegrees(angleB);
  69.         return angleB;
  70.     }
  71.     public double getAngleA()
  72.     {
  73.         double a2 = Math.pow(sideAB, 2);
  74.         double b2 = Math.pow(sideBC, 2);
  75.         double c2 = Math.pow(sideAC, 2);
  76.         double cosA = ((a2+c2-b2)/(2*sideAB*sideAC));
  77.         double angleA = Math.acos(cosA);
  78.         angleA = Math.toDegrees(angleA);
  79.         return angleA;
  80.     }
  81.     public double maxSide()
  82.     {
  83.         //if-else if-else statements for max and min sides functions
  84.         if (sideAB >= sideBC && sideAB >= sideAC)
  85.         {
  86.             return sideAB;
  87.         }
  88.         else if(sideBC >= sideAB && sideBC >= sideAC)
  89.         {
  90.             return sideBC;
  91.         }
  92.         else
  93.         {
  94.             return sideAC;
  95.         }
  96.     }
  97.     public double minSide()
  98.     {
  99.         if (sideAB <= sideBC && sideAB <= sideAC)
  100.         {
  101.             return sideAB;
  102.         }
  103.         else if(sideBC <= sideAB && sideBC <= sideAC)
  104.         {
  105.             return sideBC;
  106.         }
  107.         else
  108.         {
  109.             return sideAC;
  110.         }
  111.     }
  112.     public double maxAngle()
  113.     {
  114.         double a2 = Math.pow(sideAB, 2);
  115.         double b2 = Math.pow(sideBC, 2);
  116.         double c2 = Math.pow(sideAC, 2);
  117.         double cosC = ((b2 + c2)-a2)/((2*sideBC)*sideAC);
  118.         double angleC = Math.acos(cosC);
  119.         angleC = Math.toDegrees(angleC);
  120.         double cosB = ((a2+b2-c2)/(2*sideAB*sideBC));
  121.         double angleB = Math.acos(cosB);
  122.         angleB = Math.toDegrees(angleB);
  123.         double cosA = ((a2+c2-b2)/(2*sideAB*sideAC));
  124.         double angleA = Math.acos(cosA);
  125.         angleA = Math.toDegrees(angleA);
  126.         if (angleA >= angleB && angleA >= angleC)
  127.         {
  128.             return angleA;
  129.         }
  130.         else if(angleB >= angleA && angleB >= angleC)
  131.         {
  132.             return angleB;
  133.         }
  134.         else
  135.         {
  136.             return angleC;
  137.         }      
  138.     }
  139.     public double minAngle()
  140.     {
  141.         double a2 = Math.pow(sideAB, 2);
  142.         double b2 = Math.pow(sideBC, 2);
  143.         double c2 = Math.pow(sideAC, 2);
  144.         double cosC = ((b2 + c2)-a2)/((2*sideBC)*sideAC);
  145.         double angleC = Math.acos(cosC);
  146.         angleC = Math.toDegrees(angleC);
  147.         double cosB = ((a2+b2-c2)/(2*sideAB*sideBC));
  148.         double angleB = Math.acos(cosB);
  149.         angleB = Math.toDegrees(angleB);
  150.         double cosA = ((a2+c2-b2)/(2*sideAB*sideAC));
  151.         double angleA = Math.acos(cosA);
  152.         angleA = Math.toDegrees(angleA);
  153.         if (angleA <= angleB && angleA <= angleC)
  154.         {
  155.             return angleA;
  156.         }
  157.         else if(angleB <= angleA && angleB <= angleC)
  158.         {
  159.             return angleB;
  160.         }
  161.         else
  162.         {
  163.             return angleC;
  164.         }      
  165.     }
  166. }
  167.    
  168. public double maxAngle()
  169. {
  170.     double a2 = Math.pow(sideAB, 2);
  171.     double b2 = Math.pow(sideBC, 2);
  172.     double c2 = Math.pow(sideAC, 2);
  173.     double cosC = ((b2 + c2)-a2)/((2*sideBC)*sideAC);
  174.     double angleC = Math.acos(cosC);
  175.     angleC = Math.toDegrees(angleC);
  176.     double cosB = ((a2+b2-c2)/(2*sideAB*sideBC));
  177.     double angleB = Math.acos(cosB);
  178.     angleB = Math.toDegrees(angleB);
  179.     double cosA = ((a2+c2-b2)/(2*sideAB*sideAC));
  180.     double angleA = Math.acos(cosA);
  181.     angleA = Math.toDegrees(angleA);
  182.     if (angleA >= angleB && angleA >= angleC)
  183.     {
  184.         return angleA;
  185.     }
  186.     else if(angleB >= angleA && angleB >= angleC)
  187.     {
  188.         return angleB;
  189.     }
  190.     else
  191.     {
  192.         return angleC;
  193.     }      
  194. }
  195.    
  196. public double maxAngle() {
  197.   if(getAngleA() => getAngleB() && getAngleA() => getAngleC())
  198.     return getAngleA();
  199.   if(getAngleB() => getAngleA() && getAngleB() => getAngleC())
  200.     return getAngleB();
  201.   return getAngleC();
  202. }
  203.    
  204. private double x, y;
  205.  
  206. public double getX();
  207. public double getY();
  208. public void setX(double newX);
  209. public void setY(double newY);
  210. public void setXY(double newX, double newY);
  211. public double getDistance (Point other);
  212. public double getAngle (Point other);
  213.    
  214. private Point a, b, c;
  215.    
  216. // Can just call Point stuff directly for distances (simple code).
  217. double getDistAB() { return a.getDistance (b); }
  218. double getDistAC() { return a.getDistance (c); }
  219. double getDistBC() { return b.getDistance (c); }
  220.  
  221. double getPerimeter() { return getDistAB() + getDistAC() + getDistBC(); }
  222.  
  223. // Returns the angle inside triangle at the first vertex (complex code).
  224. double getAngleAtPointX (Point x, Point y, Point z) {
  225.     double angle = x.getAngle (y) - x.getAngle (z);
  226.     if (angle < 0)
  227.         angle = -angle;
  228.     if (angle > 180)
  229.         angle = 360 - angle;
  230.     return angle;
  231. }
  232.  
  233. // Then just call that with different arguments.
  234. double getAngleA() { return getAngleAtPoint (a, b, c); }
  235. double getAngleB() { return getAngleAtPoint (b, a, c); }
  236. double getAngleC() { return getAngleAtPoint (c, a, b); }
  237.    
  238. public class Angle {
  239.  
  240.     private double angle;
  241.  
  242.     public Angle() {
  243.  
  244.     }
  245.  
  246.     public double getAngle() {
  247.         return this.angle;
  248.     }
  249.  
  250.     public void setAngle(Side side1, Side side2, Side side3) {
  251.  
  252.         double a2 = Math.pow(side1.getLength(), 2);
  253.         double b2 = Math.pow(side2.getLength(), 2);
  254.         double c2 = Math.pow(side3.getLength(), 2);
  255.         double cosB = ((a2+b2-c2)/(2*side1.getLength()*side2.getLength()));
  256.  
  257.         double tempAngle = Math.acos(cosB);
  258.  
  259.         this.angle = Math.toDegrees(tempAngle);
  260.     }
  261.  
  262.     public Angle maxAngle(Angle angle1, Angle angle2) {
  263.  
  264.         Angle temp = angle1.getAngle() > angle2.getAngle() ? angle1 : angle2;
  265.         return temp.getAngle() > this.getAngle() ? temp : this;
  266.     }
  267.  
  268.     public Angle minAngle(Angle angle1, Angle angle2) {
  269.  
  270.         Angle temp = angle1.getAngle() < angle2.getAngle() ? angle1 : angle2;
  271.         return temp.getAngle() < this.getAngle() ? temp : this;
  272.     }
  273. }
  274.    
  275. public class Triangle {
  276.  
  277.     private Coordinate a;
  278.     private Coordinate b;
  279.     private Coordinate c;
  280.     //added these variables because I use them so frequently when calculating angles, area, perimeter, etc.
  281.  
  282.     private Side sideAB;
  283.     private Side sideBC;
  284.     private Side sideAC;
  285.  
  286.     private Angle angleA = new Angle();
  287.     private Angle angleB = new Angle();
  288.     private Angle angleC = new Angle();
  289.  
  290.     public Triangle(double x1, double y1, double x2, double y2, double x3, double y3)
  291.     {
  292.  
  293.         a = new Coordinate(x1, y1);
  294.         b = new Coordinate(x2, y2);
  295.         c = new Coordinate(x3, y3);
  296.  
  297.         sideAB= new Side(a, b);
  298.         sideBC= new Side(b, c);
  299.         sideAC= new Side(a, c);
  300.  
  301.         angleA.setAngle(sideAB, sideAC, sideBC);
  302.         angleB.setAngle(sideAB, sideBC, sideAC);
  303.         angleC.setAngle(sideAC, sideBC, sideAB);
  304.     }
  305.  
  306.     /** Your other methods to calculate Perimeter and Area **/
  307. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top