Dynamic Triangle in a Circle

Dynamic Triangle in a Circle

“Draw a circle with three random points on the circle” is a very famous assignment in your university or school. the scenario as below,

Draw a circle with three random points on the circle. Connect the points to form a triangle. Display the angles in the triangle. Use the mouse to drag a point along the perimeter of the circle. As you drag it, the triangle and angles are redisplayed dynamically as shown below.

Dynamic | Triangle | Circle | Angles | Assignment | circle with three random points

To accomplish this you have to have knowledge about mathematics, JavaFX and Math class in Java and also little understanding of Object-oriented programming.

Lets create the simple application step by step. I used Net beans as my IDE.

Firstly, open a project and create a Java Application. After that follow below 5 steps.

Step 1 – Creating an Interface named “DrawCircle”

package DrawTriangle;

import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;


/**
 *
 * @author bhanuka
 */
public interface DrawCircle {
    public void setRandomLocation(Circle tPoint, Circle c);
    
    public void updateLines(Line[] lines, Circle[] p, Text[] angles);
}

Step 2 – Creating a Class for implement the “DrawCircle”. You can see the usage of Inheritance here.

package DrawTriangle;


import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;

/**
 *
 * @author bhanuka
 */
public class DrawCircleImpl implements DrawCircle {

    @Override
    public void setRandomLocation(Circle tPoint, Circle c) {

        double angle = Math.random() * 360;
        double x = c.getCenterX() + c.getRadius() * Math.cos(Math.toRadians(angle));
        double y = c.getCenterY() + c.getRadius() * Math.sin(Math.toRadians(angle));
        tPoint.setCenterX(x);
        tPoint.setCenterY(y);

    }

    @Override
    public void updateLines(Line[] lines, Circle[] p, Text[] angles) {

        for (int i = 0; i < lines.length; i++) {

            int cIndex2 = (i + 1 >= p.length) ? 0 : i + 1;
            lines[i].setStartX(p[i].getCenterX());
            lines[i].setStartY(p[i].getCenterY());
            lines[i].setEndX(p[cIndex2].getCenterX());
            lines[i].setEndY(p[cIndex2].getCenterY());
            angles[i].setX(p[i].getCenterX() + 5);
            angles[i].setY(p[i].getCenterY() - 5);

        }

        double a = new DrawTriangleImpl().distance(lines[0]);
        double b = new DrawTriangleImpl().distance(lines[1]);
        double c = new DrawTriangleImpl().distance(lines[2]);

        double A = Math.toDegrees(Math.acos((a * a - b * b - c * c) / (-2 * b * c)));
        angles[2].setText(String.format("%.2f 1", A));

        double B = Math.toDegrees(Math.acos((b * b - a * a - c * c) / (-2 * a * c)));
        angles[0].setText(String.format("%.2f 2", B));

        double C = Math.toDegrees(Math.acos((c * c - b * b - a * a) / (-2 * a * b)));
        angles[1].setText(String.format("%.2f 3", C));
    }

}

Step 3 – Creating an Interface named “DrawTriangle”

package DrawTriangle;


import javafx.scene.shape.Line;

/**
 *
 * @author bhanuka
 */
public interface DrawTriangle {

    public double distance(Line line);

    public double distance(double x1, double y1, double x2, double y2);
}

Step 4 – Creating a Class for implement the “DrawTriangle”. You can see the usage of Inheritance here.

package DrawTriangle;


import javafx.scene.shape.Line;

/**
 *
 * @author bhanuka
 */
public class DrawTriangleImpl implements DrawTriangle{

    @Override
    public double distance(Line line) {
       return distance(line.getStartX(), line.getStartY(), line.getEndX(), line.getEndY());
    }

    @Override
    public double distance(double x1, double y1, double x2, double y2) {
        return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
    }
    
    
}

Finally, Creating a demo class to run the project.

package DrawTriangle;

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.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 *
 * @author bhanuka
 */
public class Demo extends Application {

    Pane pane = new Pane();
    double width = 500;
    double height = 500;

    @Override
    public void start(Stage stage) throws Exception {
        Circle c = new Circle(width / 2, height / 2, 150);
        c.setFill(Color.YELLOW);
        c.setStroke(Color.DARKBLUE);
        pane.getChildren().add(c);

        Circle[] circles = new Circle[3];
        Line[] lines = new Line[3];
        Text[] texts = new Text[3];

        for (int i = 0; i < circles.length; i++) {
            texts[i] = new Text();
            circles[i] = new Circle(0, 0, 5);
            new DrawCircleImpl().setRandomLocation(circles[i], c);

            final int index = i;

            circles[i].setOnMouseDragged(e -> {

                double radian = Math.atan2(e.getY() - c.getCenterY(), e.getX() - c.getCenterX());
                double x = c.getCenterX() + c.getRadius() * Math.cos(radian);
                double y = c.getCenterY() + c.getRadius() * Math.sin(radian);
                circles[index].setCenterX(x);
                circles[index].setCenterY(y);
                new DrawCircleImpl().updateLines(lines, circles, texts);
            });

        }
        
        for (int i = 0; i < lines.length; i++) {
            int cIndex2 = (i + 1 >= circles.length) ? 0 : i + 1;
            lines[i] = new Line(
                    circles[i].getCenterX(), circles[i].getCenterY(),
                    circles[cIndex2].getCenterX(), circles[cIndex2].getCenterY());

        }
        
        new DrawCircleImpl().updateLines(lines, circles, texts);
        pane.getChildren().addAll(lines);
        pane.getChildren().addAll(texts);
        pane.getChildren().addAll(circles);

        Scene scene = new Scene(pane, width, height);
        stage.setTitle("Dyanamic Triangle By Bhanu");
        stage.setScene(scene);

        stage.show();
    }

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


}

This is how the application goes. you can try this step by step. If you have any trouble on this you can clone my project from the GitHub. You can find more Java practice examples from here.