Open In App

Java Program to Implement Bowyer-Watson Algorithm

To gain some practical programming experience related to computational geometry topics, we will apply the Bowyer-Watson algorithm that will be able to facilitate the process to some extent.

Bowyer-Watson Algorithm

The Bowyer-Watson algorithm is a computational algorithm for constructing Delaunay triangulations and is also a subclass of computational geometry. Delauany ’s triangulation is one of the vital elements in computer graphics, image processing, and Finite Element Analysis.

Before going to the Java code, let's first look at how the Bowyer-Watson algorithm is summarized. The algorithm continues with the construction of a Delaunay triangulation network on the point set in a plane. It does that iteratively and always chooses the point that meets the Delaunay condition: none of the sides of this triangulation are the diameter of any circumcircles of all triangles in the triangulation.

Java Implementation

Now, we will go over the process of implementing the Bowyer-Watson algorithm in Java. We make implementation a step-by-step process.

1. Initializing the Algorithm

We begin by specifying a class named BowyerWatson with our algorithm as a member. The class will consist of the methods for point addition, triangle construction, and the Bowyer-Watson algorithm implementation.

2. Adding Points

The implementation of this by our members should enable the addition of data points to the triangulation. To realize our goal we will define the method addPoint(Point p) Points can be represented as items of a Point class that store x and y coordinates.

3. Constructing Initial Triangulation

Firstly, to use the Bowyer-Watson algorithm, we need to create a triangulation from the data provided. We can simply pick a large triangle that approximates this area. This is the base of our further triangulation.

4. Applying Bowyer-Watson Algorithm

The central implementation plan entails the use of Bowyer-Watson algorithm method. The method constructs the triangulation point by point at every step and also adjusts it as the triangulation should follow the Delaunay property.

5. Handling Edge Cases

In addition to that we need to make sure that we take care of boundary cases like points lying on the boundary or duplicate points.

6. Visualization (Optional)

In addition to this, alternative methods of displaying the triangulation can be developed by making use of Java’s graphics capabilities.

Java Program to Implement Bowyer-Watson Algorithm

Below is the implementation of Bowyer-Watson Algorithm:

// Java Program to Implement Bowyer-Watson Algorithm
import java.util.*;

// Class representing a point in 2D space
class Point {
    double x, y;

    // Constructor to initialize a point
      // with given coordinates
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    // Method to return string representation of the point
    @Override
    public String toString() {
        return "(" + x + ", " + y + ")";
    }
}

// Class representing a triangle defined by three points
class Triangle {
    Point[] vertices = new Point[3];

    // Constructor to initialize a triangle with given vertices
    public Triangle(Point p1, Point p2, Point p3) {
        vertices[0] = p1;
        vertices[1] = p2;
        vertices[2] = p3;
    }

    // Method to return string representation of the triangle
    @Override
    public String toString() {
        return "Triangle: " + vertices[0] + ", " + vertices[1] + ", " + vertices[2];
    }
}

public class BowyerWatson {

    // Method to perform the Bowyer-Watson
      // triangulation algorithm
    public static List<Triangle> triangulate(List<Point> points) {
        List<Triangle> triangulation = new ArrayList<>();

        // Create a super triangle that bounds all the points
        double minX = Double.MAX_VALUE, minY = Double.MAX_VALUE;
        double maxX = Double.MIN_VALUE, maxY = Double.MIN_VALUE;
        for (Point p : points) {
            minX = Math.min(minX, p.x);
            minY = Math.min(minY, p.y);
            maxX = Math.max(maxX, p.x);
            maxY = Math.max(maxY, p.y);
        }

        Point superPoint1 = new Point(minX - 1, minY - 1);
        Point superPoint2 = new Point(maxX + 1, minY - 1);
        Point superPoint3 = new Point((minX + maxX) / 2, maxY + 1);
        Triangle superTriangle = new Triangle(superPoint1, superPoint2, superPoint3);
        triangulation.add(superTriangle);

        // Print the input points
        System.out.println("Input Points:");
        for (Point p : points) {
            System.out.println(p);
        }
        System.out.println();

        // Print the super triangle
        System.out.println(superTriangle);

        return triangulation;
    }

    // Main method to demonstrate the Bowyer-Watson
      // triangulation algorithm
    public static void main(String[] args) {
        // Create a list of points
        List<Point> points = new ArrayList<>();
        points.add(new Point(10, 10));
        points.add(new Point(20, 20));
        points.add(new Point(30, 10));
        points.add(new Point(15, 25));

        // Perform triangulation and store the result
        List<Triangle> triangulation = triangulate(points);
    }
}

Output:

Input Points:
(10.0, 10.0)
(20.0, 20.0)
(30.0, 10.0)
(15.0, 25.0)

Triangle: (9.0, 9.0), (31.0, 9.0), (20.0, 26.0)
Article Tags :