Advertisement
Zalosin

Untitled

Dec 17th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. package proiect_geom;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.util.Scanner;
  5.  
  6. public class Proiect_geom {
  7.  
  8. public static void main(String[] args) throws FileNotFoundException {
  9. Scanner in;
  10. in = new Scanner(new java.io.File("date.in"));
  11. int n = in.nextInt();
  12. Point[] polygon = new Point[n];
  13. for (int i = 0; i < n; i++) {
  14. double x, y;
  15. x = in.nextDouble();
  16. y = in.nextDouble();
  17. polygon[i] = new Point(x, y);
  18. }
  19. Point p = new Point(in.nextDouble(), in.nextDouble());
  20. triangulate(polygon);
  21.  
  22. }
  23.  
  24. static class Point implements Comparable {
  25.  
  26. double x, y;
  27.  
  28. Point(double x, double y) {
  29. this.x = x;
  30. this.y = y;
  31. }
  32.  
  33. boolean between(Point p1, Point p2) {
  34. double minx = (p1.x < p2.x) ? p1.x : p2.x;
  35. double maxx = (p1.x > p2.x) ? p1.x : p2.x;
  36. double miny = (p1.y < p2.y) ? p1.y : p2.y;
  37. double maxy = (p1.y > p2.y) ? p1.y : p2.y;
  38. if ((this.x >= minx && this.x <= maxx) && (this.y >= miny && this.y <= maxy)) {
  39. return true;
  40. }
  41. return false;
  42. }
  43.  
  44. @Override
  45. public int compareTo(Object o) {
  46. Point p = (Point) o;
  47. if (x > p.x) {
  48. return 1;
  49. } else if (x == p.x) {
  50. if (y > p.y) {
  51. return 1;
  52. } else if (y == p.y) {
  53. return 0;
  54. } else {
  55. return -1;
  56. }
  57. }
  58. return -1;
  59. }
  60.  
  61. @Override
  62. public String toString() {
  63. return "Point{" + "x=" + x + ", y=" + y + '}';
  64. }
  65.  
  66. public Point copy() {
  67. return new Point(x, y);
  68. }
  69.  
  70. @Override
  71. public boolean equals(Object obj) {
  72. if (obj == null) {
  73. return false;
  74. }
  75. if (getClass() != obj.getClass()) {
  76. return false;
  77. }
  78. final Point other = (Point) obj;
  79. if (Double.doubleToLongBits(this.x) != Double.doubleToLongBits(other.x)) {
  80. return false;
  81. }
  82. if (Double.doubleToLongBits(this.y) != Double.doubleToLongBits(other.y)) {
  83. return false;
  84. }
  85. return true;
  86. }
  87.  
  88. }
  89.  
  90. static class Triangle {
  91.  
  92. public final Point p1, p2, p3;
  93.  
  94. Triangle(Point x, Point y, Point z) {
  95. p1 = x;
  96. p2 = y;
  97. p3 = z;
  98. }
  99.  
  100. @Override
  101. public String toString() {
  102. return "Triangle{" + "p1=" + p1 + ", p2=" + p2 + ", p3=" + p3 + '}';
  103. }
  104.  
  105. }
  106.  
  107. //TODO
  108.  
  109. private static boolean isConvex(Point p1, Point p2, Point p3)
  110. {
  111. if (area(p1,p2,p3) < 0)
  112. return true;
  113. else
  114. return false;
  115. }
  116.  
  117. //TODO
  118.  
  119. private static double area(Point p1,Point p2, Point p3)
  120. {
  121. double areaSum = 0;
  122.  
  123. areaSum += p1.x * (-p3.y + p2.y);
  124. areaSum += p2.x * (-p1.y + p3.y);
  125. areaSum += p3.x * (-p2.y + p1.y);
  126.  
  127. return areaSum;
  128. }
  129.  
  130. static Triangle[] triangulate(Point[] polygon){
  131.  
  132. boolean[] convex = new boolean[polygon.length];
  133. int n = polygon.length;
  134. convex[0] = isConvex(polygon[n-1],polygon[0],polygon[1]);
  135.  
  136. for (int i = 1; i < n-1; i++){
  137. convex[i] = isConvex(polygon[i-1],polygon[i],polygon[i+1]);
  138. }
  139. convex[n-1] = isConvex(polygon[n-2],polygon[n-1],polygon[0]);
  140. System.out.println("Convex : ");
  141. for (int i = 0; i < n; i++){
  142. System.out.println(polygon[i] + " " +convex[i]);
  143. }
  144.  
  145. return null;
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement