geometry
hand-drawn shapes
rectangles
mathematics
shape analysis

Finding properties of sloppy hand-drawn rectangles

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Rectangles are among the simplest and most fundamental geometric shapes, frequently encountered in various fields such as computer graphics, image processing, and architectural design. However, real-world scenarios often present us with sloppy, hand-drawn rectangles instead of the crisp, perfect ones produced digitally or with precision tools. This article explores the challenge of analyzing hand-drawn rectangles, detailing methods to discover their properties, and how these can differ from ideal rectangles.

Hand-Drawn vs. Ideal Rectangles

An ideal rectangle is defined mathematically by its four sides, two pairs of equal lengths, and four right angles. In contrast, a sloppy, hand-drawn rectangle may not strictly adhere to these properties:

  1. Unequal Opposite Sides: Unlike perfect rectangles, hand-drawn rectangles often feature sides that aren't perfectly equal.
  2. Skewed Angles: The angles of hand-drawn rectangles might not be precisely 90 degrees.
  3. Curved or Jagged Lines: Lines in hand-drawn rectangles may be imperfect, exhibiting curves or jags.
  4. Non-parallel Sides: Opposite sides intended to be parallel may slightly diverge or converge.

Understanding these deviations is essential in various applications, from computer vision to architectural drawings, where capturing the intent of the drawn shape is crucial.

Methods to Analyze Sloppy Hand-Drawn Rectangles

Corner Detection

Detecting the corners of the rectangle is a critical first step. Algorithms like the Harris Corner Detector or Shi-Tomasi Corner Detection can identify points in the shape that have strong gradients in orthogonal directions.

Line Fitting

Once corner points are detected, line fitting techniques such as:

Least Squares Method: This method minimizes the sum of the squared differences between points and the fitted line. It's widely used due to its simplicity and speed.

RANSAC (Random Sample Consensus): Useful in environments with a high degree of noise. It iteratively selects a random subset of data to fit a model, verifying it with the remaining data.

Determining Parallelism and Perpendicularity

Calculate the slopes of each line formed by connecting detected corners. Ideally, opposite lines should have the same slope, and adjacent lines should have slopes that are negative reciprocals. Given the imprecision in hand-drawn rectangles, allow a tolerance level when comparing these slopes.

Aspect Ratio

The aspect ratio, defined as the ratio of width to height, is a crucial property. Extracting a congruent aspect ratio can help determine the intended size and orientation of the rectangle.

Area and Orientation

With the detected corners, the area can be calculated using the shoelace formula. The orientation can be inferred from the angle each side makes with a reference axis, often useful in aligning images for further processing.

Example: Calculating Properties of a Hand-Drawn Rectangle

Consider a rectangle with corners estimated at coordinates: (1, 2), (4, 3), (3, 6), (0, 5).

Line Fitting: Applying least squares, the average slopes are obtained for each pair of points defining a line.

Aspect Ratio: Compute the distances between consecutive corners and determine the ratio of the averaged longer side to the shorter side.

Perpendicularity: Assess perpendicularity by checking the product of slopes of adjacent sides; if close to -1, they are approximately perpendicular.

Area Calculation: Use the shoelace formula:

Area=12x_1y_2+x_2y_3+x_3y_4+x_4y_1(y_1x_2+y_2x_3+y_3x_4+y_4x_1)Area = \frac{1}{2} \left| x\_1y\_2 + x\_2y\_3 + x\_3y\_4 + x\_4y\_1 - (y\_1x\_2 + y\_2x\_3 + y\_3x\_4 + y\_4x\_1) \right|

Orientation: Calculate the angle of one side relative to the x-axis.

Summary

Below is a summary of key points discussed:

PropertyIdeal RectangleHand-Drawn Rectangle
Side LengthsEqual opposite sidesMay vary slightly
AnglesExactly 90 degreesUndergo variations
LinesStraight and parallelCan be jagged or skewed
Detection MethodsSimple geometric calculationsRequire algorithms (e.g., RANSAC)
Area CalculationSimple formulaShoelace formula

Conclusion

Assessing the properties of sloppy hand-drawn rectangles demands sophisticated analysis compared to regular rectangles. Techniques in computational geometry and numerical methods are pivotal in understanding these shapes, which find applications across numerous domains. Whether used for computer-aided design interpretation or digital art enhancement, recognizing the nuances of hand-drawn rectangles enhances our capability to translate human intention into precise digital representation.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.