Skip to content

Commit fa5b3d9

Browse files
committed
add 2D geometry module
1 parent 757a088 commit fa5b3d9

1 file changed

Lines changed: 221 additions & 0 deletions

File tree

‎spatialmath/geom2d.py‎

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Created on Sun Jul 5 09:42:30 2020
5+
6+
@author: corkep
7+
"""
8+
9+
from spatialmath import base
10+
import matplotlib.pyplot as plt
11+
import numpy as np
12+
13+
# geom2d
14+
def plot_line():
15+
pass
16+
17+
def plot_box(ax=None,
18+
bbox=None, bl=None, tl=None, br=None, tr=None, wh=None, centre=None,
19+
color=None, fillcolor=None, alpha=None, thickness=None, **kwargs):
20+
"""
21+
Plot a box using matplotlib
22+
23+
:param ax: the axes to draw on, defaults to ``gca()``
24+
:type ax: Axis, optional
25+
:param bbox: bounding box matrix, defaults to None
26+
:type bbox: ndarray(2,2), optional
27+
:param bl: bottom-left corner, defaults to None
28+
:type bl: array_like(2), optional
29+
:param tl: top-left corner, defaults to None
30+
:type tl: [array_like(2), optional
31+
:param br: bottom-right corner, defaults to None
32+
:type br: array_like(2), optional
33+
:param tr: top -ight corner, defaults to None
34+
:type tr: array_like(2), optional
35+
:param wh: width and height, defaults to None
36+
:type wh: array_like(2), optional
37+
:param centre: [description], defaults to None
38+
:type centre: array_like(2), optional
39+
:param color: box outline color
40+
:type color: array_like(3) or str
41+
:param fillcolor: box fill color
42+
:type fillcolor: array_like(3) or str
43+
:param alpha: transparency, defaults to 1
44+
:type alpha: float, optional
45+
:param thickness: line thickness, defaults to None
46+
:type thickness: float, optional
47+
:return: the matplotlib object
48+
:rtype: Patch.Rectangle
49+
50+
Plots a box on the specified axes using matplotlib
51+
52+
The box can be specified in many ways:
53+
54+
- bounding box which is a 2x2 matrix [xmin, xmax; ymin, ymax]
55+
- centre and width+height
56+
- bottom-left and top-right corners
57+
- bottom-left corner and width+height
58+
- top-right corner and width+height
59+
- top-left corner and width+height
60+
"""
61+
62+
if bbox is not None:
63+
xy = bbox[:,0]
64+
w = bbox[0,1] - bbox[0,0]
65+
h = bbox[1,1] - bbox[1,0]
66+
elif bl is not None and tl is None and tr is None and wh is not None and centre is None:
67+
# bl + wh
68+
xy = bl
69+
w, h = wh
70+
elif bl is not None and tl is None and tr is not None and wh is None and centre is None:
71+
# bl + tr
72+
xy = bl
73+
w = br[0] - bl[0]
74+
h = br[1] - bl[1]
75+
elif bl is None and tl is None and tr is None and wh is not None and centre is not None:
76+
# centre + wh
77+
w, h = wh
78+
xy = (centre[0] - w / 2, centre[1] - h / 2)
79+
elif bl is None and tl is None and tr is not None and wh is not None and centre is None:
80+
# tr + wh
81+
w, h = wh
82+
xy = (tr[0] - wh[0], tr[1] - wh[1])
83+
elif bl is None and tl is not None and tr is None and wh is not None and centre is None:
84+
# tl + wh
85+
w, h = wh
86+
xy = (tl[0], tl[1] - h)
87+
88+
if ax is None:
89+
ax = plt.gca()
90+
91+
fill = fillcolor is not None
92+
rect = plt.Rectangle(xy, w, h, edgecolor=color, facecolor=fillcolor, fill=fill,
93+
alpha=alpha, linewidth=thickness, clip_on=True)
94+
ax.add_patch(rect)
95+
plt.draw()
96+
97+
return rect
98+
99+
100+
101+
102+
def plot_text(pos, text=None, ax=None, color=None, **kwargs):
103+
"""
104+
Plot text using matplotlib
105+
106+
:param pos: position of text
107+
:type pos: array_like(2)
108+
:param text: text
109+
:type text: str
110+
:param ax: axes to draw in, defaults to ``gca()``
111+
:type ax: Axis, optional
112+
:param color: text color, defaults to None
113+
:type color: str or array_like(3), optional
114+
:param kwargs: additional arguments passed to ``pyplot.text()``
115+
"""
116+
117+
defaults = {
118+
'horizontalalignment': 'left',
119+
'verticalalignment': 'center'
120+
}
121+
for k, v in defaults.items():
122+
if k not in kwargs:
123+
kwargs[k] = v
124+
if ax is None:
125+
ax = plt.gca()
126+
plt.text(pos[0], pos[1], text, color=color, **kwargs)
127+
128+
129+
130+
131+
def plot_point(pos, marker='bs', text=None, ax=None, color=None, textargs=None, **kwargs):
132+
"""
133+
Plot a point using matplotlib
134+
135+
:param pos: position of marker
136+
:type pos: array_like(2), ndarray(2,n), list of 2-tuples
137+
:param marker: matplotlub marker style, defaults to 'bs'
138+
:type marker: str or list of str, optional
139+
:param text: text label, defaults to None
140+
:type text: str, optional
141+
:param ax: axes to plot in, defaults to ``gca()````
142+
:type ax: Axis, optional
143+
:param color: text color, defaults to None
144+
:type color: str or array_like(3), optional
145+
146+
The color of the marker can be different to the color of the text,
147+
the marker color is specified by a single letter in the marker string.
148+
149+
A point can multiple markers which will be overlaid, for instance ``["rx",
150+
"ro"]`` will give a ⨂ symbol.
151+
152+
The optional text label is placed to the right of the marker, and vertically
153+
aligned.
154+
155+
Multiple points can be marked if ``pos`` is a 2xn array or a list of
156+
coordinate pairs. If a label is provided every point will have the same
157+
label. However, the text is processed with ``format`` and is provided with a
158+
single argument, the point index (starting at zero).
159+
160+
161+
"""
162+
163+
if isinstance(pos, np.ndarray) and pos.shape[0] == 2:
164+
x = pos[0,:]
165+
y = pos[1,:]
166+
elif isinstance(pos, (tuple, list)):
167+
# [x, y]
168+
# [(x,y), (x,y), ...]
169+
# [xlist, ylist]
170+
# [xarray, yarray]
171+
if base.islistof(pos, (tuple, list)):
172+
x = [z[0] for z in pos]
173+
y = [z[1] for z in pos]
174+
elif base.islistof(pos, np.ndarray):
175+
x = pos[0]
176+
y = pos[1]
177+
else:
178+
x = pos[0]
179+
y = pos[1]
180+
181+
textopts = {'fontsize': 12}
182+
if textargs is not None:
183+
textopts = {**textopts, **textargs}
184+
185+
if ax is None:
186+
ax = plt.gca()
187+
if isinstance(marker, (list, tuple)):
188+
for m in marker:
189+
plt.plot(x, y, m, **kwargs)
190+
else:
191+
plt.plot(x, y, marker)
192+
if text:
193+
try:
194+
for i, xy in enumerate(zip(x, y)):
195+
plt.text(xy[0], xy[1], ' ' + text.format(i), horizontalalignment='left', verticalalignment='center', color=color, **textopts)
196+
except:
197+
plt.text(x, y, ' ' + text, horizontalalignment='left', verticalalignment='center', color=color, **textopts)
198+
199+
200+
201+
202+
203+
def intersect_lines():
204+
# return mindist and points if no intersect
205+
pass
206+
207+
def distance_line_line():
208+
pass
209+
210+
def distance_line_point():
211+
pass
212+
213+
def points_join():
214+
215+
pass
216+
217+
def intersect_polygon___line():
218+
pass
219+
220+
def contains_polygon_point():
221+
pass

0 commit comments

Comments
 (0)