Java Program to Implement Bowyer-Watson Algorithm
Last Updated :
26 Apr, 2024
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
// 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)
Similar Reads
Java Program to Implement of Gabow Scaling Algorithm Gabow's Algorithm is a scaling algorithm that aims in solving a problem by initially considering only the highest order bit of each relevant input value (such as an edge weight). Then it refines the initial solution by looking at the two highest-order bits. It progressively looks at more and more hi
4 min read
Java Program to Implement Steepest Descent Method and Compute Local Optima The steepest descent method is an optimization algorithm that can be used to find the local minimum of a differentiable function. It is an iterative method that starts with an initial guess for the solution, and then repeatedly takes steps in the direction of the steepest descent (the direction of t
5 min read
Java Collection Programs - Basic to Advanced As it cleared from its name itself "Collection" it is a pre-defined collective bunch of classes and Interfaces present in the "Collection Framework" in Java. Their Classes, Interfaces and Methods are frequently used in competitive programming. This article provides a variety of programs on Java Coll
4 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Program to Convert English Text to Morse Code and Vice-Versa Morse code is a method used in telecommunication to encode text characters as standardized sequences of two different signal durations, called dots and dashes. Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to
5 min read
Java Program to Check the Eligibility of TPP Students for Appearing in Interviews Problem Statement: This is a Java program for placements and interview criteria such as how many students will appear for online tests, academic results, and Amcat scores, etc. As per the criteria set in the code, it is easy to check who is eligible for appearing in the MNCs like Google, Amazon, and
6 min read