SHARE
TWEET

Untitled

a guest Sep 17th, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package dip007;
  2.  
  3. import javafx.scene.shape.Circle;
  4. import org.jetbrains.annotations.NotNull;
  5.  
  6. import java.awt.*;
  7. import java.util.Scanner;
  8.  
  9. public class Main {
  10.  
  11.     static Point[] redRect = new Point[]{
  12.             new Point(3, 6),
  13.             new Point(3, 8),
  14.             new Point(11, 8),
  15.             new Point(11, 6)
  16.     };
  17.  
  18.     static Point[] blueTriangleTop = new Point[]{
  19.             new Point(4, 8),
  20.             new Point(4, 13),
  21.             new Point(9, 8)
  22.     };
  23.  
  24.     static Point[] blueTriangleBottom = new Point[]{
  25.             new Point(4, 1),
  26.             new Point(4, 6),
  27.             new Point(9, 6)
  28.     };
  29.  
  30.     static Point[] smallTopTrapezoid = new Point[]{
  31.             new Point(6, 11),
  32.             new Point(8, 11),
  33.             new Point(8, 10),
  34.             new Point(6, 10)
  35.     };
  36.  
  37.     static Point[] smallBottomTrapezoid = new Point[]{
  38.             new Point(6, 3),
  39.             new Point(7, 4),
  40.             new Point(8, 4),
  41.             new Point(8, 3)
  42.     };
  43.  
  44.     static String red = "red";
  45.     static String blue = "blue";
  46.     static String green = "green";
  47.  
  48.     public static void main(String[] args) {
  49.         Scanner input = new Scanner(System.in);
  50.         float x = 0, y = 0;
  51.         while (x == 0) {
  52.             try {
  53.                 System.out.println("enter x plz");
  54.                 x = Float.parseFloat(input.nextLine());
  55.             } catch (Exception e) {
  56.                 System.out.println("not a valid number");
  57.             }
  58.         }
  59.  
  60.         while (y == 0) {
  61.             try {
  62.                 System.out.println("enter y plz");
  63.                 y = Float.parseFloat(input.nextLine());
  64.             } catch (Exception e) {
  65.                 System.out.println("not a valid number");
  66.             }
  67.         }
  68.  
  69.         System.out.println(isPartOf(x, y, redRect));
  70.         System.out.println(isPartOf(x, y, blueTriangleTop));
  71.         System.out.println(isPartOf(x, y, blueTriangleBottom));
  72.         System.out.println(isPartOf(x, y, smallTopTrapezoid));
  73.         System.out.println(isPartOf(x, y, smallBottomTrapezoid));
  74.     }
  75.  
  76.     private static boolean contains(float x, float y, Point[] points) {
  77.         int i;
  78.         int j;
  79.         boolean result = false;
  80.         for (i = 0, j = points.length - 1; i < points.length; j = i++) {
  81.             if ((points[i].y > y) != (points[j].y > y) &&
  82.                     (x < (points[j].x - points[i].x) * (y - points[i].y) / (points[j].y - points[i].y) + points[i].x)) {
  83.                 result = !result;
  84.             }
  85.         }
  86.         return result;
  87.     }
  88.  
  89.     private static boolean isPartOf(float x, float y, @NotNull Point[] points) {
  90.         Polygon shape = new Polygon();
  91.         for (Point point : points) {
  92.             shape.addPoint(point.x, point.y);
  93.         }
  94.         return shape.contains(x, y);
  95.     }
  96.  
  97.     public static boolean isPartOfCircle(int radius, Point center, float x, float y) {
  98.         return new Circle(center.x, center.y,radius).contains(x,y);
  99.     }
  100.  
  101. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top