Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dip007;
- import javafx.scene.shape.Circle;
- import org.jetbrains.annotations.NotNull;
- import java.awt.*;
- import java.util.Scanner;
- public class Main {
- static Point[] redRect = new Point[]{
- new Point(3, 6),
- new Point(3, 8),
- new Point(11, 8),
- new Point(11, 6)
- };
- static Point[] blueTriangleTop = new Point[]{
- new Point(4, 8),
- new Point(4, 13),
- new Point(9, 8)
- };
- static Point[] blueTriangleBottom = new Point[]{
- new Point(4, 1),
- new Point(4, 6),
- new Point(9, 6)
- };
- static Point[] smallTopTrapezoid = new Point[]{
- new Point(6, 11),
- new Point(8, 11),
- new Point(8, 10),
- new Point(6, 10)
- };
- static Point[] smallBottomTrapezoid = new Point[]{
- new Point(6, 3),
- new Point(7, 4),
- new Point(8, 4),
- new Point(8, 3)
- };
- static String red = "red";
- static String blue = "blue";
- static String green = "green";
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- float x = 0, y = 0;
- while (x == 0) {
- try {
- System.out.println("enter x plz");
- x = Float.parseFloat(input.nextLine());
- } catch (Exception e) {
- System.out.println("not a valid number");
- }
- }
- while (y == 0) {
- try {
- System.out.println("enter y plz");
- y = Float.parseFloat(input.nextLine());
- } catch (Exception e) {
- System.out.println("not a valid number");
- }
- }
- System.out.println(isPartOf(x, y, redRect));
- System.out.println(isPartOf(x, y, blueTriangleTop));
- System.out.println(isPartOf(x, y, blueTriangleBottom));
- System.out.println(isPartOf(x, y, smallTopTrapezoid));
- System.out.println(isPartOf(x, y, smallBottomTrapezoid));
- }
- private static boolean contains(float x, float y, Point[] points) {
- int i;
- int j;
- boolean result = false;
- for (i = 0, j = points.length - 1; i < points.length; j = i++) {
- if ((points[i].y > y) != (points[j].y > y) &&
- (x < (points[j].x - points[i].x) * (y - points[i].y) / (points[j].y - points[i].y) + points[i].x)) {
- result = !result;
- }
- }
- return result;
- }
- private static boolean isPartOf(float x, float y, @NotNull Point[] points) {
- Polygon shape = new Polygon();
- for (Point point : points) {
- shape.addPoint(point.x, point.y);
- }
- return shape.contains(x, y);
- }
- public static boolean isPartOfCircle(int radius, Point center, float x, float y) {
- return new Circle(center.x, center.y,radius).contains(x,y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement