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 Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read