Advertisement
Guest User

smellyeli

a guest
Nov 22nd, 2017
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.83 KB | None | 0 0
  1. class Point {
  2.  
  3. private double x, y;
  4. public Point(double xx, double yy) {
  5. x = xx;
  6. y = yy;
  7. }
  8.  
  9. public double getX() {
  10. return x;
  11. }
  12.  
  13. public double getY() {
  14. return y;
  15. }
  16.  
  17. public double getDistance(Point point) {
  18.  
  19. double distance = Math.sqrt(Math.pow(x - point.getX(), 2) + Math.pow(y - point.getY(), 2));
  20. return distance;
  21. }
  22. }
  23. abstract class Shape {
  24. public abstract double getArea();
  25. public abstract double getPerimeter();
  26. }
  27.  
  28. interface Symmetric {
  29. public Point getPointOfSymmetry();
  30. }
  31. class Triangle extends Shape {
  32. Point firstPoint, secondPoint, thirdPoint;
  33. public Triangle(Point fPoint, Point sPoint, Point tPoint) {
  34. firstPoint = fPoint;
  35. secondPoint = sPoint;
  36. thirdPoint = tPoint;
  37. }
  38. public Point getFirstPoint() {
  39. return firstPoint;
  40. }
  41.  
  42. public Point getSecondPoint() {
  43. return secondPoint;
  44. }
  45.  
  46. public Point getThirdPoint() {
  47. return thirdPoint;
  48. }
  49.  
  50. @Override
  51. public double getArea() {
  52. // area = abs((Ax(By-Cy) + Bx(Cy-Ay) + Cx(Ay-By))/2)
  53. // https://www.mathopenref.com/coordtrianglearea.html
  54. double area = Math.abs((firstPoint.getX() * (secondPoint.getY() - thirdPoint.getY()) +
  55. secondPoint.getX() * (thirdPoint.getY() - firstPoint.getY()) +
  56. thirdPoint.getX() * (firstPoint.getY() - secondPoint.getY())) / 2);
  57. return area;
  58. }
  59.  
  60. @Override
  61. public double getPerimeter() {
  62. double firstToSecondPoint = firstPoint.getDistance(secondPoint);
  63. double secondToThirdPoint = secondPoint.getDistance(thirdPoint);
  64. double thirdToFirstPoint = thirdPoint.getDistance(firstPoint);
  65. return firstToSecondPoint + secondToThirdPoint + thirdToFirstPoint;
  66. }
  67. }
  68. class Rectangle extends Shape {
  69. Point topLeftPoint;
  70. double length, width;
  71.  
  72. public Rectangle(Point tLeftPoint, double l, double w) {
  73. topLeftPoint = tLeftPoint;
  74. length = l;
  75. width = w;
  76. }
  77.  
  78. public Point getTopLeftPoint() {
  79. return topLeftPoint;
  80. }
  81.  
  82. public double getLength() {
  83. return length;
  84. }
  85.  
  86. public double getWidth() {
  87. return width;
  88. }
  89.  
  90. @Override
  91. public double getArea() {
  92. return length * width;
  93. }
  94.  
  95. @Override
  96. public double getPerimeter() {
  97. return 2 * (width + length);
  98. }
  99. }
  100. class Circle extends Shape implements Symmetric {
  101.  
  102. Point center;
  103. double radius;
  104.  
  105. public Circle(Point c, double r) {
  106. center = c;
  107. radius = r;
  108. }
  109.  
  110. public Point getCenter() {
  111. return center;
  112. }
  113.  
  114. public double getRadius() {
  115. return radius;
  116. }
  117.  
  118. @Override
  119. public double getArea() {
  120. return Math.PI * radius * radius;
  121. }
  122.  
  123. @Override
  124. public double getPerimeter() {
  125. return 2 * Math.PI * radius;
  126. }
  127.  
  128. @Override
  129. public Point getPointOfSymmetry() {
  130. //return the center
  131. return center;
  132. }
  133.  
  134. }
  135. class Trapezoid extends Shape {
  136.  
  137. Point topLeftPoint, bottomLeftPoint;
  138. double topSide, bottomSide;
  139. public Trapezoid(Point tlp, Point blp, double ts, double bs) {
  140. topLeftPoint = tlp;
  141. bottomLeftPoint = blp;
  142. topSide = ts;
  143. bottomSide = bs;
  144. }
  145.  
  146. public Point getTopLeftPoint() {
  147. return topLeftPoint;
  148. }
  149.  
  150. public Point getBottomLeftPoint() {
  151. return bottomLeftPoint;
  152. }
  153.  
  154. public double getTopSide() {
  155. return topSide;
  156. }
  157.  
  158. public double getBottomSide() {
  159. return bottomSide;
  160. }
  161.  
  162. @Override
  163. public double getArea() {
  164. // lets determine the point in bottom line to get height
  165. Point aPoint = new Point(topLeftPoint.getX(), bottomLeftPoint.getY());
  166. double height = topLeftPoint.getDistance(aPoint);
  167. double area = height * (topSide + bottomSide) / 2;
  168. return area;
  169. }
  170.  
  171. @Override
  172. public double getPerimeter() {
  173. double leftTopToBottomTop = topLeftPoint.getDistance(bottomLeftPoint);
  174. Point rightTopSide = new Point(topLeftPoint.getX() + topSide, topLeftPoint.getY());
  175. Point rightBottomSide = new Point(bottomLeftPoint.getX() + bottomSide, bottomLeftPoint.getY());
  176. double rightTopToBottomTop = rightTopSide.getDistance(rightBottomSide);
  177. return leftTopToBottomTop + rightTopToBottomTop + topSide + bottomSide;
  178. }
  179. }
  180. class EquilateralTriangle extends Triangle implements Symmetric{
  181.  
  182. double side;
  183. public EquilateralTriangle(Point topPoint, double s){
  184. super(topPoint,new Point(topPoint.getX(),topPoint.getY()-s),new Point(topPoint.getX()+s,topPoint.getY()-s));
  185. side = s;
  186. }
  187.  
  188. public Point getTopPoint(){
  189. return firstPoint;
  190. }
  191.  
  192. public double getSide(){
  193. return side;
  194. }
  195.  
  196. @Override
  197. public Point getPointOfSymmetry(){
  198. Point midPointBase = new Point(secondPoint.getX() + side/2, secondPoint.getY());
  199. double distanceFromMidToTopPoint = midPointBase.getDistance(firstPoint);
  200.  
  201. Point symPoint = new Point(midPointBase.getX(),midPointBase.getY() - distanceFromMidToTopPoint/3);
  202. return symPoint;
  203. }
  204. }
  205.  
  206.  
  207. class Square extends Rectangle implements Symmetric {
  208. public Square(Point topLeft, double side) {
  209. super(topLeft, side, side);
  210. }
  211. public double getSide() {
  212. return super.getLength();
  213. }
  214.  
  215. @Override
  216. public Point getPointOfSymmetry() {
  217. //This should return the centre
  218. double symX = topLeftPoint.getX() + width / 2;
  219. double symY = topLeftPoint.getY() - width / 2;
  220.  
  221. Point p = new Point(symX, symY);
  222. return p;
  223. }
  224. }
  225.  
  226.  
  227.  
  228. class Plane {
  229. private Shape[] shapes = new Shapes[0];
  230.  
  231. public Plane() {}
  232.  
  233. private int getShapeCounter() {
  234. return shapes.length;
  235. }
  236. public Shape[] getShape() {
  237. return shapes;
  238. }
  239.  
  240. public void addShape(Shape shape) {
  241. Shape[] newShapes = new Shape[shapes.length + 1];
  242.  
  243. for (int i = 0; i < shapes.length; i++)
  244. newShapes[i] = shapes[i];
  245.  
  246. newShapes[shapeList.length] = shape;
  247. shapes = newShapes;
  248. }
  249.  
  250. public double getSumOfAreas() {
  251. double totalArea = 0;
  252. for (int i = 0; i < getShapeCounter(); i++) {
  253. totalArea += shapes[i].getArea();
  254. }
  255. return totalArea;
  256. }
  257.  
  258. public double getSumOfPerimeters() {
  259. double totalPerimeter = 0;
  260. for (int i = 0; i < getShapeCounter(); i++) {
  261. totalPerimeter += shapes[i].getArea();
  262. }
  263. return totalPerimeter;
  264. }
  265.  
  266. public Point getCenterOfPointOfSymmetries() {
  267. double xSym = 0;
  268. double ySym = 0;
  269. int count = 0;
  270. for (int i = 0; i < getShapeCounter(); i++) {
  271. if (shapes[i].getClass() == EquilateralTriangle.class) {
  272. EquilateralTriangle et = (EquilateralTriangle) shapes[i];
  273. xSym += et.getPointOfSymmetry().getX();
  274. ySym += et.getPointOfSymmetry().getY();
  275. count++;
  276. } else if (shapes[i].getClass() == Circle.class) {
  277. Circle c = (Circle) shapes[i];
  278. xSym += c.getPointOfSymmetry().getX();
  279. ySym += c.getPointOfSymmetry().getY();
  280. count++;
  281. } else if (shapes[i].getClass() == Square.class) {
  282. Square s = (Square) shapes[i];
  283. xSym += s.getPointOfSymmetry().getX();
  284. ySym += s.getPointOfSymmetry().getY();
  285. count++;
  286. }
  287. }
  288.  
  289. // Get the average
  290. xSym = xSym / count;
  291. ySym = ySym / count;
  292.  
  293. Point p = new Point(xSym, ySym);
  294. return p;
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement