Points Inside Figure - Java 2D Graphics

Java examples for 2D Graphics:Path

Description

Points Inside Figure

Demo Code

import java.awt.geom.Path2D;
import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    double[] xCoordinates = new double[] { 12.5, 22.5, 22.5, 20, 20, 17.5, 17.5, 12.5 };
    double[] yCoordinates = new double[] { 6, 6, 13.5, 13.5, 8.5, 8.5, 13.5, 13.5 };

    Double xCheck = (double) 0;
    Double yCheck = (double) 0;
    Scanner input = new Scanner(System.in);

    xCheck = input.nextDouble();//from w ww.  j ava 2 s  . c  o m
    yCheck = input.nextDouble();
    input.close();

    Path2D poli = new Path2D.Double();
    poli.moveTo(xCoordinates[0], yCoordinates[0]);
    for (int i = 1; i < xCoordinates.length; i++) {
      poli.lineTo(xCoordinates[i], yCoordinates[i]);
    }

    if (poli.contains(xCheck, yCheck)) {
      System.out.println("Inside");
    } else {
      System.out.println("Outside");
    }
  }
}

Related Tutorials