Search code examples
javarandom2ddrawinggeometry

How can we separate the points inside and outside a circle in java?


I have a question about Circle Inside/Outside. First of all we have 9 circle and we have to generate many random 2D points (say N=100,000) and draw them in red if they are within any of these circles. Otherwise, draw them in grey color.

I tried to do a few things and I got this result:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;


public class circlejava extends Application{
    public void start(Stage primaryStage) {
        // scene parameters
        int scene_w = 600;
        int scene_h = 600;

        // circle parameters
        long radius = 150;
        int center_x = 300;
        int center_y = 300;

        Pane pane = new Pane();
        // Some code to drawing circle
        Circle circle = new Circle();
        circle.setCenterX(center_x);
        circle.setCenterY(center_y);
        circle.setRadius(radius);
        circle.setStroke(Color.RED);
        circle.setStrokeWidth(1);
        circle.setFill(null);
        pane.getChildren().add(circle);

        Circle circle2 = new Circle();
        circle2.setCenterX(100);
        circle2.setCenterY(100);
        circle2.setRadius(50);
        circle2.setStroke(Color.RED);
        circle2.setStrokeWidth(1);
        circle2.setFill(null);
        pane.getChildren().add(circle2);

        Circle circle3 = new Circle();
        circle3.setCenterX(300);
        circle3.setCenterY(100);
        circle3.setRadius(20);
        circle3.setStroke(Color.RED);
        circle3.setStrokeWidth(1);
        circle3.setFill(null);
        pane.getChildren().add(circle3);

        Circle circle4 = new Circle();
        circle4.setCenterX(500);
        circle4.setCenterY(100);
        circle4.setRadius(50);
        circle4.setStroke(Color.RED);
        circle4.setStrokeWidth(1);
        circle4.setFill(null);
        pane.getChildren().add(circle4);

        Circle circle5 = new Circle();
        circle5.setCenterX(center_x);
        circle5.setCenterY(center_y);
        circle5.setRadius(radius);
        circle5.setStroke(Color.RED);
        circle5.setStrokeWidth(1);
        circle5.setFill(null);
        pane.getChildren().add(circle5);

        Circle circle6 = new Circle();
        circle6.setCenterX(500);
        circle6.setCenterY(300);
        circle6.setRadius(20);
        circle6.setStroke(Color.RED);
        circle6.setStrokeWidth(1);
        circle6.setFill(null);
        pane.getChildren().add(circle6);

        Circle circle7 = new Circle();
        circle7.setCenterX(100);
        circle7.setCenterY(300);
        circle7.setRadius(20);
        circle7.setStroke(Color.RED);
        circle7.setStrokeWidth(1);
        circle7.setFill(null);
        pane.getChildren().add(circle7);

        Circle circle8 = new Circle();
        circle8.setCenterX(100);
        circle8.setCenterY(500);
        circle8.setRadius(50);
        circle8.setStroke(Color.RED);
        circle8.setStrokeWidth(1);
        circle8.setFill(null);
        pane.getChildren().add(circle8);

        Circle circle9 = new Circle();
        circle9.setCenterX(300);
        circle9.setCenterY(500);
        circle9.setRadius(20);
        circle9.setStroke(Color.RED);
        circle9.setStrokeWidth(1);
        circle9.setFill(null);
        pane.getChildren().add(circle9);

        Circle circle10 = new Circle();
        circle10.setCenterX(500);
        circle10.setCenterY(500);
        circle10.setRadius(50);
        circle10.setStroke(Color.RED);
        circle10.setStrokeWidth(1);
        circle10.setFill(null);
        pane.getChildren().add(circle10);

        for(int i = 0; i<100000; i++) {

            Circle nokta = new Circle();
            double t = 2 * Math.PI * Math.random();
            double r = Math.sqrt(Math.random());
            double x = r * Math.cos(t);
            double y = r * Math.sin(t);
            nokta.setCenterX(x*scene_w);
            nokta.setCenterY(y*scene_h);
            nokta.setRadius(r);
            nokta.setFill(Color.RED);
            nokta.setStroke(Color.GRAY);
            pane.getChildren().add(nokta);
        }


        Scene scene = new Scene(pane, scene_w, scene_h);
        primaryStage.setTitle("Pi Estimation by Random Points");
        primaryStage.setScene(scene);// Place the scene in the stage
        primaryStage.setResizable(false);
        primaryStage.show();// Display the stage

        }


    public static void main(String[] args){
       Application.launch(args);

    }


}

So, How can I separate the points inside and outside the circle ?

Thank you,


Solution

  • Since the radius of the circles are known and also their coordinates of their center is known.

    You will need a way to store the radius and coordinates of the circle's center point for each circle.

    Each time a random point is generated, then it is checked against each circle's center point coordinates if it is in the range (i.e. randomPoint <= radius) of the circle.

    If a random point is within the range (or inside) of one of the circle's center points, then the random point is red, otherwise (or outside all the circles) it is grey.

    You may need a class and create some objects with an array, as a suggestion.

    Any questions and/or clarifications. Let me know in comment and will do my best.

    ------- edit 2/16/2018 ----------

    Some checks to keep in mind:

    1) Is there a set radii for the 2D random points so they are visible to the human eye?

    2) Are the random points x and y coordinates in the range of the size of the pane?

    3) Is there a function that checks if the random point is in the circle?

    4) Is there or if the math is correct to check if the random point(s) is inside the circle?