Creating point, line and polygon — Shape4D 0.08 documentation

# Point, line and polygon

## Point

With the aid of Python collections, such as set/list, and the package numpy, we can hold the coordinates for 3D points as well as 3D lines and 3D polygons.
For the case of a single point, we can set a point with a set, that is,

    Point = (x,y)
    Point = np.array((x,y))

For the case of multiple points, we can set points with a list of sets, that is,

    Points = [(a,b,c),(d,e,f),(g,h,i)]
    Points = np.array([(a,b,c),(d,e,f),(g,h,i)])

For instance, there are two single points (1,2) and (2, 2), and one group of points (0.0,0.0),(0.5,0.0),(1.0,0.0),(1.5,0.0) and (2.0,0.0). The Python code is listed as follows,

    >>> P1 = (1,1)           #  Creating point (1,1)
    >>> P2 = np.array ((2,2))
    >>> Points1 = [(0.0,0.0),(0.5,0.0),(1.0,0.0),(1.5,0.0),(2.0,0.0)]
    >>> Points2 = np.array([(0.0,0.0),(0.5,0.0),(1.0,0.0),(1.5,0.0),(2.0,0.0)])

Further more, they can also be represented by a matrix for some convenience.

    >>> Points1 = [[(0.0,0.0),(0.5,0.0)],[(1.0,0.0),(1.5,0.0)],[(2.0,0.0),(2.5,0.0)]]
    >>> Points2 = np.array([[(0.0,0.0),(0.5,0.0)],[(1.0,0.0),(1.5,0.0)],[(2.0,0.0),(2.5,0.0)]])

## Line and plane

A line is represented by both a point and a slope.

    >>> P,L = (0,0), (1,2)

It means a line across (0,0) with slope of (1,2).

A plane is represented by a point and its normal vector.

    >>> P,n = (0,0,0 ) , (1,2,3)

It means a plane across (0,0,0) with a normal vector of (1,2,3).

## Segment

A segment can be set by two points.

    >>> P1,P2 = (0,0), (1,1)

It defines a segment from (0,0) to (1,1).

## Polygon

Here, the polygon is close, meaning that its starting point is also its ending point. Its vertices run counter-clockwise as viewed toward its facet. With the methods as stated above, we can create a 3D polygon directly

    >>> vertices = [(0,0,0),(1,0,0),(1,1,1),(0,1,1)]
    >>> polygon = np.array(vertices)

As shown above, we have to provide a set of vertices running counter-clockwise to enclose a polygon, a method that is prone to make mistake by chance. To simplify the process, we have designed a flexible way in Shape4D: one can first define polygon in a 2D plane, and then move and/or rotate it to the designated position.
In the following, for example, we create a 2D polygon first in yz plane and then transform it to its real 3D position as desired.

    >>> vertices2D = Vertices([(0,0),(0,2**0.5),(1,2**0.5),(1,0)])    # verices are defined by (y,z).

#### Transformation

In general, a transformation is accomplished by rotation together with translation.
Here, the rotation of a polygon is designed to control with two parameters, α and β, as shown below. In the rotating process, its origin is always fixed at the origin (0,0,0).
α is made from y axis to its first edge moving counter-clockwise in the xy plane around z axis, as viewed against the direction of z;
β is made from z axis to its projection on the facet of polygon after it turns clockwise around its first edge, as viewed against the direction of the edge.

Translation of a polygon means that its origin moves to a new position.

In Shape4D, the rotation and translation are combined into one function move().

    >>> import Shape4D.Shapes as shape
    >>> x0,y0,z0 = 0.3,0.4,0.5
    >>> alpha, beta = -90,45
    >>> vertices = shape.move(shape = vertices2D, to = (x0,y0,z0), by = (alpha,beta))