Create a new regular polygon. .. hint:: If no rotation is specified there is always a point at ``(length, 0)`` If no center is specified, the center will be calculated such that all the vertexes positive and the bounding box inc
(cls, sides, length, start_rads = None, start_degs = None, center = None)
| 166 | |
| 167 | @classmethod |
| 168 | def from_regular(cls, sides, length, start_rads = None, start_degs = None, center = None): |
| 169 | """ |
| 170 | Create a new regular polygon. |
| 171 | |
| 172 | .. hint:: |
| 173 | |
| 174 | If no rotation is specified there is always a point at ``(length, 0)`` |
| 175 | |
| 176 | If no center is specified, the center will be calculated such that |
| 177 | all the vertexes positive and the bounding box includes (0, 0). This |
| 178 | operation requires O(n) time (where n is the number if sides) |
| 179 | |
| 180 | May specify the angle of the first point. For example, if the coordinate |
| 181 | system is x to the right and y upward, then if the starting offset is 0 |
| 182 | then the first point will be at the right and the next point counter-clockwise. |
| 183 | |
| 184 | This would make for the regular quad (sides=4) to look like a diamond. To make |
| 185 | the bottom side a square, the whole polygon needs to be rotated 45 degrees, like |
| 186 | so: |
| 187 | |
| 188 | .. code-block:: python |
| 189 | |
| 190 | from pygorithm.geometry import (vector2, polygon2) |
| 191 | import math |
| 192 | |
| 193 | # This is a diamond shape (rotated square) (0 degree rotation assumed) |
| 194 | diamond = polygon2.Polygon2.from_regular(4, 1) |
| 195 | |
| 196 | # This is a flat square |
| 197 | square = polygon2.Polygon2.from_regular(4, 1, start_degs = 45) |
| 198 | |
| 199 | # Creating a flat square with radians |
| 200 | square2 = polygon2.Polygon2.from_regular(4, 1, math.pi / 4) |
| 201 | |
| 202 | Uses the `definition of a regular polygon <https://en.wikipedia.org/wiki/Regular_polygon>` |
| 203 | to find the angle between each vertex in the polygon. Then converts the side |
| 204 | length to circumradius using the formula explained `here <http://mathworld.wolfram.com/RegularPolygon.html>` |
| 205 | |
| 206 | Finally, each vertex is found using ``<radius * cos(angle), radius * sin(angle)>`` |
| 207 | |
| 208 | If the center is not specified, the minimum of the bounding box of the |
| 209 | polygon is calculated while the vertices are being found, and the inverse |
| 210 | of that value is offset to the rest of the points in the polygon. |
| 211 | |
| 212 | :param sides: the number of sides in the polygon |
| 213 | :type sides: :class:`numbers.Number` |
| 214 | :param length: the length of any side of the polygon |
| 215 | :type length: :class:`numbers.Number` |
| 216 | :param start_rads: the starting radians or None |
| 217 | :type start_rads: :class:`numbers.Number` or None |
| 218 | :param start_degs: the starting degrees or None |
| 219 | :type start_degs: :class:`numbers.Number` or None |
| 220 | :param center: the center of the polygon |
| 221 | :type center: :class:`pygorithm.geometry.vector2.Vector2` |
| 222 | :returns: the new regular polygon |
| 223 | :rtype: :class:`pygorithm.geometry.polygon2.Polygon2` |
| 224 | |
| 225 | :raises ValueError: if ``sides < 3`` or ``length <= 0`` |
no outgoing calls