Skip to content

Commit eb078bc

Browse files
authored
no error on zero length line
Removes the error for projecting to a zero length line with no clamping. Instead it behaves just like a clamped line.
1 parent 4194dce commit eb078bc

File tree

3 files changed

+4
-10
lines changed

3 files changed

+4
-10
lines changed

docs/reST/ref/geometry.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@
762762

763763
Example of what the clamp argument changes. If it is `True`, the point is bounded between the line segment ends.
764764

765-
WARNING: This method has to have some length or the clamp parameter must be true for it to work and not throw a `ValueError`
765+
WARNING: If the line has no length (i.e. the start and end points are the same) then the returned point of this function will be the same point as both ends of the line.
766766

767767
.. versionadded:: 2.5.6
768768

src_c/line.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,18 +227,13 @@ _line_project_helper(pgLineBase *line, double *point, int clamp)
227227
double squared_line_length =
228228
line_vector[0] * line_vector[0] + line_vector[1] * line_vector[1];
229229

230-
if (squared_line_length == 0.0 && clamp) {
230+
if (squared_line_length == 0.0) {
231231
double projected_point[2];
232232
projected_point[0] = line->ax;
233233
projected_point[1] = line->ay;
234234
return pg_tuple_couple_from_values_double(projected_point[0],
235235
projected_point[1]);
236236
}
237-
else if (squared_line_length == 0.0) {
238-
return RAISE(PyExc_ValueError,
239-
"The Line has to have some length or this method has to "
240-
"be clamped to work");
241-
}
242237

243238
// this is a vector that goes from the start of the line to the point we
244239
// are projecting onto the line

test/geometry_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,9 +2231,8 @@ def test_meth_project(self):
22312231
self.assertEqual(math.ceil(projected_point[0]), 0)
22322232
self.assertEqual(math.ceil(projected_point[1]), 0)
22332233

2234-
# testing if the method fails when it should
2235-
with self.assertRaises(ValueError):
2236-
bad_line.project(test_bad_line_point)
2234+
projected_point = bad_line.project(test_bad_line_point)
2235+
self.assertEqual(math.ceil(projected_point[0]), 0)
22372236

22382237
def test__str__(self):
22392238
"""Checks whether the __str__ method works correctly."""

0 commit comments

Comments
 (0)