API reference - Class DPolygon

Notation used in Ruby API documentation

Description: A polygon class

A polygon consists of an outer hull and zero to many holes. Each contour consists of several points. The point list is normalized such that the leftmost, lowest point is the first one. The orientation is normalized such that the orientation of the hull contour is clockwise, while the orientation of the holes is counterclockwise.

It is in no way checked that the contours are not overlapping. This must be ensured by the user of the object when filling the contours.

A polygon can be asked for the number of holes using the holes method. each_point_hull delivers the points of the hull contour. each_point_hole delivers the points of a specific hole. each_edge delivers the edges (point-to-point connections) of both hull and holes. bbox delivers the bounding box, area the area and perimeter the perimeter of the polygon.

Here's an example of how to create a polygon:

hull =  [ RBA::DPoint::new(0, 0),       RBA::DPoint::new(6000, 0), 
          RBA::DPoint::new(6000, 3000), RBA::DPoint::new(0, 3000) ]
hole1 = [ RBA::DPoint::new(1000, 1000), RBA::DPoint::new(2000, 1000), 
          RBA::DPoint::new(2000, 2000), RBA::DPoint::new(1000, 2000) ]
hole2 = [ RBA::DPoint::new(3000, 1000), RBA::DPoint::new(4000, 1000), 
          RBA::DPoint::new(4000, 2000), RBA::DPoint::new(3000, 2000) ]
poly = RBA::DPolygon::new(hull)
poly.insert_hole(hole1)
poly.insert_hole(hole2)

# ask the polygon for some properties
poly.holes      # -> 2
poly.area       # -> 16000000.0
poly.perimeter  # -> 26000.0
poly.bbox       # -> (0,0;6000,3000)

The DPolygon class stores coordinates in floating-point format which gives a higher precision for some operations. A class that stores integer coordinates is Polygon.

See The Database API for more details about the database objects.

Public constructors

new DPolygonnewDefault constructor: creates an empty (invalid) polygon
new DPolygonnew(const DSimplePolygon sp)Constructor from a simple polygon
new DPolygonnew(DPoint[] pts)Constructor given the points of the polygon hull
new DPolygonnew(const DBox box)Constructor converting a box to a polygon

Public methods

[const]bool!=(const DPolygon p)Inequality test
[const]DPolygon*(double f)Scaling by some factor
[const]bool<(const DPolygon p)Less operator
[const]bool==(const DPolygon p)Equality test
[const]doubleareaThe area of the polygon
[const]voidassign(const DPolygon other)Assign the contents of another object to self
voidassign_hole(unsigned int n,
DPoint[] p)
Set the points of the given hole of the polygon
voidassign_hole(unsigned int n,
const DBox b)
Set the box as the given hole of the polygon
voidassign_hull(DPoint[] p)Set the points of the hull of polygon
[const]DBoxbboxReturn the bounding box of the polygon
voidcompress(bool remove_reflected)Compress the polygon.
voidcreateEnsures the C++ object is created
voiddestroyExplicitly destroy the object
[const]booldestroyed?Returns a value indicating whether the object was already destroyed
[const]DPolygondupCreates a copy of self
[iter]DEdgeeach_edgeIterate over the edges that make up the polygon
[const,iter]DPointeach_point_hole(unsigned int n)Iterate over the points that make up the nth hole
[const,iter]DPointeach_point_hullIterate over the points that make up the hull
[const]unsigned intholesReturns the number of holes
voidhull=(DPoint[] p)Set the points of the hull of polygon
voidinsert_hole(DPoint[] p)Insert a hole with the given points
voidinsert_hole(const DBox b)Insert a hole from the given box
[const]boolinside?(DPoint p)Test, if the given point is inside the polygon
[const]boolis_box?Returns true, if the polygon is a simple box.
[const]boolis_const_object?Returns a value indicating whether the reference is a const reference
DPolygonmove(const DPoint p)Moves the polygon.
DPolygonmove(double x,
double y)
Moves the polygon.
[const]DPolygonmoved(const DPoint p)Returns the moved polygon (does not modify self)
[const]DPolygonmoved(double x,
double y)
Returns the moved polygon (does not modify self)
unsigned intnum_pointsGet the total number of points (hull plus holes)
unsigned intnum_points_hole(unsigned int n)Get the number of points of the given hole
unsigned intnum_points_hullGet the number of points of the hull
[const]doubleperimeterThe perimeter of the polygon
DPointpoint_hole(unsigned int n,
unsigned int p)
Get a specific point of a hole
DPointpoint_hull(unsigned int p)Get a specific point of the hull
voidsize(double dx,
double dy,
unsigned int mode)
Sizing (biasing)
voidsize(double d,
unsigned int mode)
Sizing (biasing)
voidsize(double d)Sizing (biasing)
[const]DPolygonsized(double dx,
double dy,
unsigned int mode)
Sizing (biasing) without modifying self
[const]DPolygonsized(double d,
unsigned int mode)
Sizing (biasing) without modifying self
[const]DPolygonsized(double d)Sizing (biasing) without modifying self
[const]stringto_sConvert to a string
[const]DPolygontransformed(const DTrans t)Transform the polygon
[const]DPolygontransformed(const DCplxTrans t)Transform the polygon with a complex transformation

Public static methods and constants

new DPolygonellipse(const DBox box,
int n)
Creates a simple polygon appoximating an ellipse
DPolygonfrom_ipoly(const Polygon int_poly)Construct a floating-point coordinate polygon from an integer coordinate one
new DPolygonfrom_s(string s)Creates an object from a string

Deprecated methods (protected, public, static, non-static and constructors)

[const]boolinside(DPoint p)Use of this method is deprecated. Use inside? instead
[static]new DPolygonnew_b(const DBox box)Use of this method is deprecated. Use new instead
[static]new DPolygonnew_p(DPoint[] pts)Use of this method is deprecated. Use new instead
unsigned intpointsUse of this method is deprecated. Use num_points instead
unsigned intpoints_hole(unsigned int n)Use of this method is deprecated. Use num_points_hole instead
unsigned intpoints_hullUse of this method is deprecated. Use num_points_hull instead
[const]DPolygontransformed_cplx(const DCplxTrans t)Use of this method is deprecated. Use transformed instead

Detailed description

[const] bool !=(const DPolygon p)

Description: Inequality test

p:The object to compare against

[const] DPolygon *(double f)

Description: Scaling by some factor

Returns the scaled object. All coordinates are multiplied with the given factor and if necessary rounded.

[const] bool <(const DPolygon p)

Description: Less operator

p:The object to compare against

This operator is provided to establish some, not necessarily a certain sorting order

[const] bool ==(const DPolygon p)

Description: Equality test

p:The object to compare against

[const] double area

Description: The area of the polygon

The area is correct only if the polygon is not self-overlapping and the polygon is oriented clockwise.Orientation is ensured automatically in most cases.

[const] void assign(const DPolygon other)

Description: Assign the contents of another object to self

This method assigns the contents of another object to self. This is a deep copy that does not only copy the reference but the actual content.

void assign_hole(unsigned int n,DPoint[] p)

Description: Set the points of the given hole of the polygon

n:The index of the hole to which the points should be assigned
p:An array of points to assign to the polygon's hole

If the hole index is not valid, this method does nothing. This method was introduced in version 0.18.

void assign_hole(unsigned int n,const DBox b)

Description: Set the box as the given hole of the polygon

n:The index of the hole to which the points should be assigned
b:The box to assign to the polygon's hole

If the hole index is not valid, this method does nothing. This method was introduced in version 0.23.

void assign_hull(DPoint[] p)

Description: Set the points of the hull of polygon

p:An array of points to assign to the polygon's hull

The 'assign_hull' variant is provided in analogy to 'assign_hole'.

[const] DBox bbox

Description: Return the bounding box of the polygon

The bounding box is the box enclosing all points of the polygon.

void compress(bool remove_reflected)

Description: Compress the polygon.

remove_reflected:See description of the functionality.

This method removes redundant points from the polygon, such as points being on a line formed by two other points. If remove_reflected is true, points are also removed if the two adjacent edges form a spike.

This method was introduced in version 0.18.

void create

Description: Ensures the C++ object is created

Use this method to ensure the C++ object is created, for example to ensure that resources are allocated. Usually C++ objects are created on demand and not necessarily when the script object is created.

void destroy

Description: Explicitly destroy the object

Explicitly destroy the object on C++ side if it was owned by the Ruby interpreter. Subsequent access to this object will throw an exception. If the object is not owned by Ruby, this method will do nothing.

[const] bool destroyed?

Description: Returns a value indicating whether the object was already destroyed

This method returns true, if the object was destroyed, either explicitly or by the C++ side. The latter may happen, if the object is owned by a C++ object which got destroyed itself.

[const] DPolygon dup

Description: Creates a copy of self

[iter] DEdge each_edge

Description: Iterate over the edges that make up the polygon

This iterator will deliver all edges, including those of the holes. Hole edges are oriented counterclockwise while hull egdes are oriented clockwise.

[const,iter] DPoint each_point_hole(unsigned int n)

Description: Iterate over the points that make up the nth hole

The hole number must be less than the number of holes (see holes)

[const,iter] DPoint each_point_hull

Description: Iterate over the points that make up the hull

[static] new DPolygon ellipse(const DBox box,int n)

Description: Creates a simple polygon appoximating an ellipse

box:The bounding box of the ellipse
n:The number of points that will be used to approximate the ellipse

This method has been introduced in version 0.23.

[static] DPolygon from_ipoly(const Polygon int_poly)

Description: Construct a floating-point coordinate polygon from an integer coordinate one

This method has been added in version 0.15.

[static] new DPolygon from_s(string s)

Description: Creates an object from a string

Creates the object from a string representation (as returned by to_s)

This method has been added in version 0.23.

[const] unsigned int holes

Description: Returns the number of holes

void hull=(DPoint[] p)

Description: Set the points of the hull of polygon

p:An array of points to assign to the polygon's hull

The 'assign_hull' variant is provided in analogy to 'assign_hole'.

void insert_hole(DPoint[] p)

Description: Insert a hole with the given points

p:An array of points to insert as a new hole

void insert_hole(const DBox b)

Description: Insert a hole from the given box

b:The box to insert as a new hole

This method was introduced in version 0.23.

[const] bool inside(DPoint p)

Description: Test, if the given point is inside the polygon

This method is deprecated. Use method 'inside?' instead

[const] bool inside?(DPoint p)

Description: Test, if the given point is inside the polygon

If the given point is inside or on the edge of the polygon, true is returned. This tests works well only if the polygon is not self-overlapping and oriented clockwise.

[const] bool is_box?

Description: Returns true, if the polygon is a simple box.

Returns:True if the polygon is a box.

A polygon is a box if it is identical to it's bounding box.

This method was introduced in version 0.23.

[const] bool is_const_object?

Description: Returns a value indicating whether the reference is a const reference

This method returns true, if self is a const reference. In that case, only const methods may be called on self.

DPolygon move(const DPoint p)

Description: Moves the polygon.

p:The distance to move the polygon.
Returns:The moved polygon (self).

Moves the polygon by the given offset and returns the moved polygon. The polygon is overwritten.

This method has been introduced in version 0.23.

DPolygon move(double x,double y)

Description: Moves the polygon.

x:The x distance to move the polygon.
y:The y distance to move the polygon.
Returns:The moved polygon (self).

Moves the polygon by the given offset and returns the moved polygon. The polygon is overwritten.

[const] DPolygon moved(const DPoint p)

Description: Returns the moved polygon (does not modify self)

p:The distance to move the polygon.
Returns:The moved polygon.

Moves the polygon by the given offset and returns the moved polygon. The polygon is not modified.

This method has been introduced in version 0.23.

[const] DPolygon moved(double x,double y)

Description: Returns the moved polygon (does not modify self)

x:The x distance to move the polygon.
y:The y distance to move the polygon.
Returns:The moved polygon.

Moves the polygon by the given offset and returns the moved polygon. The polygon is not modified.

This method has been introduced in version 0.23.

[static] new DPolygon new

Description: Default constructor: creates an empty (invalid) polygon

[static] new DPolygon new(const DSimplePolygon sp)

Description: Constructor from a simple polygon

sp:The simple polygon that is converted into the polygon

This method was introduced in version 0.22.

[static] new DPolygon new(DPoint[] pts)

Description: Constructor given the points of the polygon hull

pts:The points forming the polygon hull

[static] new DPolygon new(const DBox box)

Description: Constructor converting a box to a polygon

box:The box to convert to a polygon

[static] new DPolygon new_b(const DBox box)

Description: Constructor converting a box to a polygon

box:The box to convert to a polygon

This method is deprecated. Use method 'new' instead

[static] new DPolygon new_p(DPoint[] pts)

Description: Constructor given the points of the polygon hull

pts:The points forming the polygon hull

This method is deprecated. Use method 'new' instead

unsigned int num_points

Description: Get the total number of points (hull plus holes)

This method was introduced in version 0.18.

unsigned int num_points_hole(unsigned int n)

Description: Get the number of points of the given hole

The argument gives the index of the hole of which the number of points are requested. The index must be less than the number of holes (see holes).

unsigned int num_points_hull

Description: Get the number of points of the hull

[const] double perimeter

Description: The perimeter of the polygon

The perimeter is sum of the lengths of all edges making up the polygon.

This method has been introduce in version 0.23.

DPoint point_hole(unsigned int n,unsigned int p)

Description: Get a specific point of a hole

n:The index of the hole to which the points should be assigned
p:The index of the point to get

If the index of the point or of the hole is not valid, a default value is returned. This method was introduced in version 0.18.

DPoint point_hull(unsigned int p)

Description: Get a specific point of the hull

p:The index of the point to get

If the index of the point is not a valid index, a default value is returned. This method was introduced in version 0.18.

unsigned int points

Description: Get the total number of points (hull plus holes)

This method is deprecated. Use method 'num_points' instead

unsigned int points_hole(unsigned int n)

Description: Get the number of points of the given hole

This method is deprecated. Use method 'num_points_hole' instead

unsigned int points_hull

Description: Get the number of points of the hull

This method is deprecated. Use method 'num_points_hull' instead

void size(double dx,double dy,unsigned int mode)

Description: Sizing (biasing)

Shifts the contour outwards (dx,dy>0) or inwards (dx,dy<0). dx is the sizing in x-direction and dy is the sizing in y-direction. The sign of dx and dy should be identical. The sizing operation create invalid (self-overlapping, reverse oriented) contours.

The mode defines at which bending angle cutoff occurs (0:>0, 1:>45, 2:>90, 3:>135, 4:>approx. 168, other:>approx. 179)

In order to obtain a proper polygon in the general case, the sized polygon must be merged in 'greater than zero' wrap count mode. This is necessary since in the general case, sizing can be complicated operation which lets a single polygon fall apart into disjoint pieces for example. This can be achieved using the EdgeProcessor class for example:

poly = ... # a RBA::Polygon
poly.size(-50, 2)
ep = RBA::EdgeProcessor::new
# result is an array of RBA::Polygon objects
result = ep.simple_merge_p2p([ poly ], false, false, 1)

void size(double d,unsigned int mode)

Description: Sizing (biasing)

Shifts the contour outwards (d>0) or inwards (d<0). This method is equivalent to

size(d, d, mode)

See size for a detailed description.

This method has been introduced in version 0.23.

void size(double d)

Description: Sizing (biasing)

Shifts the contour outwards (d>0) or inwards (d<0). This method is equivalent to

size(d, d, 2)

See size for a detailed description.

[const] DPolygon sized(double dx,double dy,unsigned int mode)

Description: Sizing (biasing) without modifying self

This method applies sizing to the polygon but does not modify self. Instead a sized copy is returned. See size for a description of the operation.

This method has been introduced in version 0.23.

[const] DPolygon sized(double d,unsigned int mode)

Description: Sizing (biasing) without modifying self

Shifts the contour outwards (d>0) or inwards (d<0). This method is equivalent to

sized(d, d, mode)

See size and sized for a detailed description.

[const] DPolygon sized(double d)

Description: Sizing (biasing) without modifying self

This method is equivalent to

sized(d, d, 2)

See size and sized for a detailed description.

[const] string to_s

Description: Convert to a string

[const] DPolygon transformed(const DTrans t)

Description: Transform the polygon

t:The transformation to apply.
Returns:The transformed polygon.

Transforms the polygon with the given transformation. Does not modify the polygon but returns the transformed polygon.

[const] DPolygon transformed(const DCplxTrans t)

Description: Transform the polygon with a complex transformation

t:The transformation to apply.
Returns:The transformed polygon.

Transforms the polygon with the given complex transformation. Does not modify the polygon but returns the transformed polygon.

[const] DPolygon transformed_cplx(const DCplxTrans t)

Description: Transform the polygon with a complex transformation

t:The transformation to apply.
Returns:The transformed polygon.

This method is deprecated. Use method 'transformed' instead