This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from mpewsey/v1.1.0
Add polygon
- Loading branch information
Showing
10 changed files
with
127 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# polygon_ex1.py | ||
from xsect import polygon_points, plot_section | ||
|
||
points = polygon_points(5, 1, 0.1) | ||
plot_section(points, title='Polygon') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# This module configures matplotlib | ||
|
||
import os | ||
import matplotlib | ||
|
||
if 'DISPLAY' not in os.environ: | ||
matplotlib.use('Agg') | ||
|
||
import matplotlib.pyplot as plt | ||
|
||
__all__ = ['plt'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from __future__ import division | ||
import numpy as np | ||
from .boundary import close_points, section_summary | ||
|
||
__all__ = ['polygon_points', 'polygon_summary'] | ||
|
||
|
||
def polygon_points(n, radius, thickness=None, is_inscribed=True): | ||
""" | ||
Returns an array of boundary points for a polygon. | ||
Parameters | ||
---------- | ||
n : int | ||
The number of sides to the polygon. | ||
radius : float | ||
The radius of the polygon. | ||
thickness : float | ||
The thickness of the wall. If None, the cross section will be assumed | ||
to be solid. | ||
is_inscribed : bool | ||
If True, an inscribed polygon will be generated for the specified | ||
radius. Otherwise, a circumscribed polygon will be generated. | ||
""" | ||
c = np.cos(np.pi/n) | ||
|
||
if is_inscribed: | ||
ro = radius | ||
else: | ||
ro = radius / c | ||
|
||
ang = np.linspace(0.5*np.pi, 2.5*np.pi, n+1) | ||
ang = np.column_stack([np.cos(ang), np.sin(ang)]) | ||
points = ro * ang | ||
|
||
if thickness is not None: | ||
if is_inscribed: | ||
ri = ro - thickness / c | ||
else: | ||
ri = (radius - thickness) / c | ||
|
||
p = ri * ang[::-1] | ||
points = np.concatenate([points, p]) | ||
|
||
return close_points(points) | ||
|
||
|
||
def polygon_summary(n, radius, thickness=None, is_inscribed=True): | ||
""" | ||
Returns a dictionary with a summary of polygon section properties. | ||
Parameters | ||
---------- | ||
n : int | ||
The number of sides to the polygon. | ||
radius : float | ||
The radius of the polygon. | ||
thickness : float | ||
The thickness of the wall. If None, the cross section will be assumed | ||
to be solid. | ||
is_inscribed : bool | ||
If True, an inscribed polygon will be generated for the specified | ||
radius. Otherwise, a circumscribed polygon will be generated. | ||
""" | ||
p = polygon_points(n, radius, thickness, is_inscribed) | ||
return section_summary(p) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import pytest | ||
import numpy as np | ||
from .polygon import * | ||
|
||
|
||
def test_polygon_summary(): | ||
# Inscribed | ||
p = polygon_summary(6, 1, is_inscribed=True) | ||
assert pytest.approx(p['area']) == 2.5980762113533 | ||
|
||
p = polygon_summary(6, 1, 0.1, is_inscribed=True) | ||
a = 2.5980762113533 - 2.0327172275047 | ||
assert pytest.approx(p['area']) == a | ||
|
||
# Circumscribed | ||
p = polygon_summary(6, 1, is_inscribed=False) | ||
assert pytest.approx(p['area']) == 3.4641016151378 | ||
|
||
p = polygon_summary(6, 1, 0.1, is_inscribed=False) | ||
a = 3.4641016151378 - 2.8059223082616 | ||
assert pytest.approx(p['area']) == a |