Geometric Shape Validation and Computational Geometry in Java

This implementation provides a robust foundation for validating and computing properties of planar geometric shapes—specifically quadrilaterals and convex polygons—using object-oriented design principles in Java.

The core logic separates concerns across multiple specialized classes:

  • InputValidator handles input parsing, format checking, and point count validation using regular expressions and strict numeric parsing.
  • Point encapsulates 2D coordinates with bounds enforcement (0 < x, y ≤ 200) and utility methods for distance and orientation.
  • Polygon serves as an abstract base for n-gons, defining common interfaces for convexity testing, area calculation via the shoelace formula, and perimeter computation.
  • Concrete subclasses like Quadrilateral and Pentagon extend Polygon, implementing shape-specific logic such as parallleogram detection (checking opposite side equality), rhombus verification (all sides equal), rectangle classification (equal diagonals and right angles), and convex hull traversal.
  • LineSegment models line segments with intersection detection using cross-product sign checks and bounding-box prefiltering. It supports coincidence detection and precise intersection counting.
  • GeometryContainer introduces collection management via ArrayList<Element>, suporting dynamic insertion (add()), indexed removal (remove(int index)), and polymorphic iteration.

Key computational features include:

  • Convexity determination by evaluating consistent winding direction using cross products of consecutive edge vectors.
  • Area calculation via decomposition into triangles anchored at the first vertex, summing signed areas with absolute value application.
  • Robust floating-point output formatting: values are rounded to three decimal places only when necessary, preserving trailing zeros in the fractional part only up to the third digit.
  • Edge-case handling: overlapping or collinear points trigger "points coincide"; malformed input yields "Wrong Format"; insufficient point counts produce "wrong number of points".

The architecture enforces separation of concerns: parsing logic resides in validators, geometric logic lives in shape classes, and container operations are centralized. All display methods adhere to strict formatting rules, and all numeric computations use double-precision arithmetic with defensive bounds checking.

Example usage for convex quadrilateral analysis:

public class ShapeAnalyzer {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        
        InputValidator validator = new InputValidator(input);
        if (!validator.isValid()) {
            System.out.println(validator.getErrorMessage());
            return;
        }
        
        int option = validator.getOption();
        List<Point> points = validator.getPoints();
        
        switch (option) {
            case 1:
                Quadrilateral quad = new Quadrilateral(points);
                System.out.printf("%s %s\n",
                    quad.isValid() ? "true" : "false",
                    quad.isParallelogram() ? "true" : "false"
                );
                break;
            case 3:
                if (!quad.isValid()) {
                    System.out.println("not a quadrilateral");
                } else {
                    System.out.printf("%s %.3f %.3f\n",
                        quad.isConvex() ? "true" : "false",
                        quad.getPerimeter(),
                        quad.getArea()
                    );
                }
                break;
        }
    }
}

All shape classes inherit from Element, enabling runtime polymorphism through method overriding of display(). This allows unified rendering of points (x,y), lines with endpoints and length, and planes with color—all respecting precision constraints.

Tags: computational-geometry java-oop polygon-validation convex-hull geometric-algorithms

Posted on Mon, 11 May 2026 09:29:29 +0000 by Kaylub