Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Regular_Polygon Class Reference

A regular polygon defined by center, side length, and vertex count. More...

#include <polygon.H>

Inheritance diagram for Regular_Polygon:
[legend]
Collaboration diagram for Regular_Polygon:
[legend]

Classes

class  Segment_Iterator
 Iterator over the edges (segments) of a regular polygon. More...
 
class  Vertex_Iterator
 Iterator over the vertices of a regular polygon. More...
 

Public Member Functions

 Regular_Polygon ()
 Default constructor. Creates an invalid empty polygon.
 
 Regular_Polygon (const Point &c, const double &side_sz, const size_t &n, const double &ang=0)
 Construct a regular polygon.
 
const doubleget_side_size () const
 Get the side length.
 
const Pointget_center () const
 Get the center point.
 
const size_tsize () const
 Get the number of vertices.
 
const doubleradius () const
 Get the circumradius.
 
bool is_closed () const
 Check if the polygon is closed.
 
const Point get_vertex (const size_t &i) const
 Get the i-th vertex of the polygon.
 
const Point get_first_vertex () const
 Get the first vertex (index 0).
 
const Point get_last_vertex () const
 Get the last vertex (index size()-1).
 
Segment get_first_segment () const
 Get the first segment (edge).
 
Segment get_last_segment () const
 Get the last segment (closing edge).
 
Point lowest_point () const
 Get the lowest point of the bounding circle.
 
Point highest_point () const
 Get the highest point of the bounding circle.
 
Point leftmost_point () const
 Get the leftmost point of the bounding circle.
 
Point rightmost_point () const
 Get the rightmost point of the bounding circle.
 
- Public Member Functions inherited from Geom_Object
 Geom_Object ()=default
 
virtual ~Geom_Object ()=default
 

Private Attributes

Point center
 Center point of the polygon.
 
double side_size
 Length of each side.
 
size_t num_vertex
 Number of vertices (sides)
 
double angle
 Rotation angle in radians.
 
double r
 Circumradius (distance from center to vertices)
 
double beta
 Central angle between adjacent vertices (2π/n)
 

Detailed Description

A regular polygon defined by center, side length, and vertex count.

Regular_Polygon represents an n-sided regular polygon (equilateral and equiangular) defined parametrically by its center point, the length of each side, and the number of vertices.

Construction

The polygon is constructed using the law of sines to calculate the circumradius from the side length:

double beta
Central angle between adjacent vertices (2π/n)
Definition polygon.H:835
double side_size
Length of each side.
Definition polygon.H:831
double r
Circumradius (distance from center to vertices)
Definition polygon.H:834
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_sin_function > > sin(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4070
DynList< T > maps(const C &c, Op op)
Classic map operation.

Where:

  • beta = 2π/n (central angle)
  • alpha = (π - beta)/2 (angle between radius and side)

Vertex Numbering

Vertices are numbered 0 to n-1. Vertex 0 is positioned at angle angle from the negative y-axis (i.e., pointing "south" when angle=0).

Example

// Create a regular hexagon centered at (100, 100) with side length 50
Regular_Polygon hexagon(Point(100, 100), 50.0, 6);
// Iterate over vertices
for (size_t i = 0; i < hexagon.size(); ++i)
std::cout << hexagon.get_vertex(i).to_string() << "\n";
// Create rotated square (45 degrees = PI/4 radians)
Regular_Polygon diamond(Point(0, 0), 100.0, 4, PI/4);
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
Rectangular point in the plane.
Definition point.H:156
A regular polygon defined by center, side length, and vertex count.
Definition polygon.H:829
constexpr double PI
Definition point.H:85
See also
Polygon

Definition at line 828 of file polygon.H.

Constructor & Destructor Documentation

◆ Regular_Polygon() [1/2]

Regular_Polygon::Regular_Polygon ( )
inline

Default constructor. Creates an invalid empty polygon.

Definition at line 839 of file polygon.H.

◆ Regular_Polygon() [2/2]

Regular_Polygon::Regular_Polygon ( const Point c,
const double side_sz,
const size_t n,
const double ang = 0 
)
inline

Construct a regular polygon.

Parameters
cCenter point of the polygon.
side_szLength of each side.
nNumber of vertices (must be >= 3).
angRotation angle in radians (default 0).
Exceptions
std::domain_errorIf n < 3.

Definition at line 851 of file polygon.H.

References ah_domain_error_if, beta, Aleph::maps(), PI, r, side_size, and sin().

Member Function Documentation

◆ get_center()

const Point & Regular_Polygon::get_center ( ) const
inline

Get the center point.

Returns
Reference to the center point.

Definition at line 872 of file polygon.H.

References center.

Referenced by TEST_F().

◆ get_first_segment()

Segment Regular_Polygon::get_first_segment ( ) const
inline

Get the first segment (edge).

Returns
Segment from vertex 0 to vertex 1.

Definition at line 920 of file polygon.H.

References get_vertex().

◆ get_first_vertex()

const Point Regular_Polygon::get_first_vertex ( ) const
inline

Get the first vertex (index 0).

Returns
The first vertex point.

Definition at line 912 of file polygon.H.

References get_vertex().

◆ get_last_segment()

Segment Regular_Polygon::get_last_segment ( ) const
inline

Get the last segment (closing edge).

Returns
Segment from vertex 0 to last vertex.

Definition at line 927 of file polygon.H.

References get_vertex(), and num_vertex.

◆ get_last_vertex()

const Point Regular_Polygon::get_last_vertex ( ) const
inline

Get the last vertex (index size()-1).

Returns
The last vertex point.

Definition at line 916 of file polygon.H.

References get_vertex(), and num_vertex.

◆ get_side_size()

const double & Regular_Polygon::get_side_size ( ) const
inline

Get the side length.

Returns
Length of each side.

Definition at line 868 of file polygon.H.

References side_size.

Referenced by TEST_F(), and TEST_F().

◆ get_vertex()

const Point Regular_Polygon::get_vertex ( const size_t i) const
inline

Get the i-th vertex of the polygon.

Vertex 0 is at angle angle from the negative y-axis. Subsequent vertices are placed at equal angular intervals counter-clockwise.

Parameters
iVertex index (0 to size()-1).
Returns
The vertex point.
Exceptions
std::out_of_rangeIf i >= size().

Definition at line 895 of file polygon.H.

References ah_out_of_range_error_if, angle, beta, center, Segment::get_tgt_point(), num_vertex, r, and Segment::rotate().

Referenced by build_poly_graph(), Polygon::copy_regular_polygon(), Regular_Polygon::Segment_Iterator::get_current_segment(), Regular_Polygon::Vertex_Iterator::get_current_vertex(), get_first_segment(), get_first_vertex(), get_last_segment(), get_last_vertex(), TEST(), TEST_F(), and TEST_F().

◆ highest_point()

Point Regular_Polygon::highest_point ( ) const
inline

Get the highest point of the bounding circle.

Returns
Point at the top of the circumscribed circle.

Definition at line 1070 of file polygon.H.

References center, and r.

◆ is_closed()

bool Regular_Polygon::is_closed ( ) const
inline

Check if the polygon is closed.

Returns
Always true for regular polygons.

Definition at line 884 of file polygon.H.

◆ leftmost_point()

Point Regular_Polygon::leftmost_point ( ) const
inline

Get the leftmost point of the bounding circle.

Returns
Point at the left of the circumscribed circle.

Definition at line 1074 of file polygon.H.

References center, and r.

◆ lowest_point()

Point Regular_Polygon::lowest_point ( ) const
inline

Get the lowest point of the bounding circle.

Returns
Point at the bottom of the circumscribed circle.
Note
For simplicity, returns cardinal points of the circumcircle, not the actual extreme vertices.

Definition at line 1066 of file polygon.H.

References center, and r.

◆ radius()

const double & Regular_Polygon::radius ( ) const
inline

Get the circumradius.

Returns
Distance from center to each vertex.

Definition at line 880 of file polygon.H.

References r.

Referenced by TEST(), TEST_F(), and TEST_F().

◆ rightmost_point()

Point Regular_Polygon::rightmost_point ( ) const
inline

Get the rightmost point of the bounding circle.

Returns
Point at the right of the circumscribed circle.

Definition at line 1078 of file polygon.H.

References center, and r.

◆ size()

const size_t & Regular_Polygon::size ( ) const
inline

Member Data Documentation

◆ angle

double Regular_Polygon::angle
private

Rotation angle in radians.

Definition at line 833 of file polygon.H.

Referenced by get_vertex().

◆ beta

double Regular_Polygon::beta
private

Central angle between adjacent vertices (2π/n)

Definition at line 835 of file polygon.H.

Referenced by Regular_Polygon(), and get_vertex().

◆ center

Point Regular_Polygon::center
private

Center point of the polygon.

Definition at line 830 of file polygon.H.

Referenced by get_center(), get_vertex(), highest_point(), leftmost_point(), lowest_point(), and rightmost_point().

◆ num_vertex

size_t Regular_Polygon::num_vertex
private

Number of vertices (sides)

Definition at line 832 of file polygon.H.

Referenced by get_last_segment(), get_last_vertex(), get_vertex(), and size().

◆ r

double Regular_Polygon::r
private

Circumradius (distance from center to vertices)

Definition at line 834 of file polygon.H.

Referenced by Regular_Polygon(), get_vertex(), highest_point(), leftmost_point(), lowest_point(), radius(), and rightmost_point().

◆ side_size

double Regular_Polygon::side_size
private

Length of each side.

Definition at line 831 of file polygon.H.

Referenced by Regular_Polygon(), and get_side_size().


The documentation for this class was generated from the following file: