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 Playfair Cipher Algorithm
Cipher is an algorithm for encryption and decryption. The cipher text is a process that applies to different types of algorithms to convert plain text to coded text. It is referred to as ciphertext. The Playfair cipher was the first practical digraph substitution cipher. The scheme was invented in 1
6 min read
Java Program to Implement ZhuâTakaoka String Matching Algorithm
Zhu-Takaoka String Matching Algorithm is a Variant of Boyer Moore Algorithm for Pattern Matching in a String. There is a slight change in the concept of Bad Maps in this algorithm. The concept of Good Suffixes remains as same as that of Boyer Moore's but instead of using a single character for Bad S
7 min read
Java Program to Implement Wagner and Fisher Algorithm for Online String Matching
The Wagner-Fischer Algorithm is a dynamic programming algorithm that measures the Levenshtein distance or the edit distance between two strings of characters. Levenshtein Distance(LD) calculates how similar are the two strings. The distance is calculated by three parameters to transform the string1
3 min read
Implementing Rabin Karp Algorithm Using Rolling Hash in Java
There are so many pattern searching algorithms for the string. KMP algorithm, Z algorithm Rabin Karp algorithm, etc these algorithms are the optimization of Naive Pattern searching Algorithm. Naive Pattern Searching Algorithm: Input : "AABACACAACAC" Pattern : "CAC" Output : [4,9] AABACACAACAC Implem
5 min read
Java Program to Search an Element in Vector
A vector in Java is a dynamic array that can be resized as needed. It is synchronized, which means it is safe in multi-threaded programs. To find an element in a Vector we have to loop through its elements to find a match. In this article, we will learn how to search for a component in a Vector usin
3 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 Array Programs
An array is a data structure consisting of a collection of elements (values or variables), of the same memory size, each identified by at least one array index or key. An array is a linear data structure that stores similar elements (i.e. elements of similar data type) that are stored in contiguous
4 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