1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
# Copyright (c) 2010-2024, Manfred Moitzi
# License: MIT License
import pytest
from math import radians
import numpy as np
from ezdxf.math import xround, Vec2
from ezdxf.math.construct2d import *
def test_left_of_line():
assert is_point_left_of_line(Vec2(-1, 0), Vec2(0, 0), Vec2(0.1, 1)) is True
assert is_point_left_of_line(Vec2(1, 0), Vec2(0, 0), Vec2(0, -1)) is True
assert is_point_left_of_line(Vec2(-1, -1), Vec2(0, 0), Vec2(0.1, 1)) is True
def test_point_to_line_relation_left():
assert point_to_line_relation(Vec2(-1, 0), Vec2(0, 0), Vec2(0.1, 1)) == -1
assert point_to_line_relation(Vec2(1, 0), Vec2(0, 0), Vec2(0, -1)) == -1
assert point_to_line_relation(Vec2(-1, -1), Vec2(0, 0), Vec2(0.1, 1)) == -1
def test_left_of_line_or_on_the_line():
# vertical line
assert (
is_point_left_of_line(Vec2(1, 0), Vec2(0, 0), Vec2(0, 1), colinear=True)
is False
)
assert (
is_point_left_of_line(
Vec2(0, 0.5), Vec2(0, 0), Vec2(0, 1), colinear=True
)
is True
)
assert (
is_point_left_of_line(
Vec2(-1, 0.5), Vec2(0, 0), Vec2(0, 1), colinear=True
)
is True
)
# horizontal line
assert (
is_point_left_of_line(Vec2(0, 1), Vec2(0, 0), Vec2(1, 0), colinear=True)
is True
)
assert (
is_point_left_of_line(Vec2(0, 0), Vec2(0, 0), Vec2(1, 0), colinear=True)
is True
)
assert (
is_point_left_of_line(
Vec2(0, -1), Vec2(0, 0), Vec2(1, 0), colinear=True
)
is False
)
# 45 deg line
assert (
is_point_left_of_line(Vec2(0, 0), Vec2(0, 0), Vec2(1, 1), colinear=True)
is True
)
assert (
is_point_left_of_line(
Vec2(0.5, 0.5), Vec2(0, 0), Vec2(1, 1), colinear=True
)
is True
)
assert (
is_point_left_of_line(Vec2(1, 1), Vec2(0, 0), Vec2(1, 1), colinear=True)
is True
)
assert (
is_point_left_of_line(
Vec2(0.5, 0.49), Vec2(0, 0), Vec2(1, 1), colinear=True
)
is False
)
def test_point_ot_line_relation_on_line():
# vertical line
assert point_to_line_relation(Vec2(0, 2), Vec2(0, 0), Vec2(0, 1)) == 0
assert point_to_line_relation(Vec2(0, -1), Vec2(0, 0), Vec2(0, 1)) == 0
assert point_to_line_relation(Vec2(0, 0.5), Vec2(0, 0), Vec2(0, 1)) == 0
# horizontal line
assert point_to_line_relation(Vec2(1, 0), Vec2(0, 0), Vec2(1, 0)) == 0
assert point_to_line_relation(Vec2(-1, 0), Vec2(0, 0), Vec2(1, 0)) == 0
assert point_to_line_relation(Vec2(0, 0), Vec2(0, 0), Vec2(1, 0)) == 0
# 45 deg line
assert point_to_line_relation(Vec2(0, 0), Vec2(0, 0), Vec2(1, 1)) == 0
assert point_to_line_relation(Vec2(0.5, 0.5), Vec2(0, 0), Vec2(1, 1)) == 0
assert point_to_line_relation(Vec2(1, 1), Vec2(0, 0), Vec2(1, 1)) == 0
assert point_to_line_relation(Vec2(-0.5, -0.5), Vec2(0, 0), Vec2(1, 1)) == 0
def test_xround():
assert xround(9.999, 0.0) == 10
assert xround(9.999, 0.5) == 10
assert xround(9.75, 0.5) == 10
assert xround(9.74, 0.5) == 9.5
assert xround(9.74, 0.25) == 9.75
assert xround(9.626, 0.25) == 9.75
assert xround(9.624, 0.25) == 9.50
assert xround(9.624, 0.1) == 9.6
assert xround(9.624, 0.01) == 9.62
assert xround(9.626, 0.01) == 9.63
assert xround(9.626, 0.05) == 9.65
assert xround(19.1, 1.0) == 19
assert xround(19.1, 2.0) == 20
assert xround(18.9, 2.0) == 18
assert xround(18.9, 5.0) == 20
assert xround(18.9, 10) == 20
assert xround(1234, 10) == 1230
assert xround(1236, 10) == 1240
def test_enclosing_angles():
assert (
enclosing_angles(
radians(45),
start_angle=radians(45),
end_angle=radians(45),
ccw=True,
)
is True
)
assert (
enclosing_angles(
radians(45),
start_angle=radians(45),
end_angle=radians(45),
ccw=False,
)
is True
)
assert (
enclosing_angles(
radians(90),
start_angle=radians(45),
end_angle=radians(135),
ccw=True,
)
is True
)
assert (
enclosing_angles(
radians(90),
start_angle=radians(45),
end_angle=radians(135),
ccw=False,
)
is False
)
assert (
enclosing_angles(
radians(0),
start_angle=radians(45),
end_angle=radians(135),
ccw=True,
)
is False
)
assert (
enclosing_angles(
radians(0),
start_angle=radians(45),
end_angle=radians(135),
ccw=False,
)
is True
)
assert (
enclosing_angles(
radians(45),
start_angle=radians(50),
end_angle=radians(40),
ccw=True,
)
is False
)
assert (
enclosing_angles(
radians(45),
start_angle=radians(50),
end_angle=radians(40),
ccw=False,
)
is True
)
assert (
enclosing_angles(
radians(90),
start_angle=radians(135),
end_angle=radians(45),
ccw=True,
)
is False
)
assert (
enclosing_angles(
radians(90),
start_angle=radians(135),
end_angle=radians(45),
ccw=False,
)
is True
)
assert (
enclosing_angles(
radians(270),
start_angle=radians(135),
end_angle=radians(45),
ccw=True,
)
is True
)
assert (
enclosing_angles(
radians(270),
start_angle=radians(135),
end_angle=radians(45),
ccw=False,
)
is False
)
def test_no_points():
assert closest_point((0, 0), []) is None
def test_one_points():
assert closest_point((0, 0), [(1, 1)]) == (1, 1)
def test_two_points():
assert closest_point((0, 0), [(0, 0, 1), (1, 1, 1)]) == (0, 0, 1)
def test_more_points():
assert closest_point(
(0, 0), [(0, 0, 1), (1, 1, 1), (2, 2, 2), (0, 0, -0.5)]
) == (0, 0, -0.5)
def test_decdeg2dms():
ss = 1.0 / 3600.0
mm = 1.0 / 60.0
for value in np.linspace(-2, 2, 71):
d, m, s = decdeg2dms(value)
result = d + m * mm + s * ss
assert value == pytest.approx(result)
assert decdeg2dms(-1) == (-1, 0, 0)
assert decdeg2dms(0) == (0, 0, 0)
assert decdeg2dms(1) == (1, 0, 0)
def test_linspace():
assert list(np.linspace(1, 4, num=4)) == [1, 2, 3, 4]
assert list(np.linspace(1, 4, num=1)) == [1]
assert list(np.linspace(1, 4, num=0)) == []
assert list(np.linspace(1, 5, num=4, endpoint=False)) == [1, 2, 3, 4]
assert list(np.linspace(2, -2, num=5)) == [2, 1, 0, -1, -2]
with pytest.raises(ValueError):
list(np.linspace(1, 4, num=-1))
def test_area():
assert area([(4, 6), (4, -4), (8, -4), (8, -8), (-4, -8), (-4, 6)]) == 128
|