Skip to content

Commit

Permalink
Fix line rotate (#238)
Browse files Browse the repository at this point in the history
* Fix line rotate.

* fix tests
  • Loading branch information
itzpr3d4t0r authored Oct 19, 2024
1 parent 353a26c commit 3c11f8e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
20 changes: 10 additions & 10 deletions src_c/line.c
Original file line number Diff line number Diff line change
Expand Up @@ -754,8 +754,8 @@ _pg_rotate_line_helper(pgLineBase *line, double angle, double rx, double ry)

double angle_rad = DEG_TO_RAD(angle);

double x1 = line->x1, y1 = line->y1;
double x2 = line->x2, y2 = line->y2;
double x1 = line->xa, y1 = line->ya;
double x2 = line->xb, y2 = line->yb;

double cos_a = cos(angle_rad);
double sin_a = sin(angle_rad);
Expand All @@ -770,11 +770,11 @@ _pg_rotate_line_helper(pgLineBase *line, double angle, double rx, double ry)
double x2_new = x2 * cos_a - y2 * sin_a;
double y2_new = x2 * sin_a + y2 * cos_a;

line->x1 = x1_new + rx;
line->y1 = y1_new + ry;
line->xa = x1_new + rx;
line->ya = y1_new + ry;

line->x2 = x2_new + rx;
line->y2 = y2_new + ry;
line->xb = x2_new + rx;
line->yb = y2_new + ry;
}

static PyObject *
Expand All @@ -787,8 +787,8 @@ pg_line_rotate(pgLineObject *self, PyObject *const *args, Py_ssize_t nargs)
pgLineBase *line = &self->line;
double angle, rx, ry;

rx = (line->x1 + line->x2) / 2;
ry = (line->y1 + line->y2) / 2;
rx = (line->xa + line->xb) / 2;
ry = (line->ya + line->yb) / 2;

if (!pg_DoubleFromObj(args[0], &angle)) {
return RAISE(PyExc_TypeError,
Expand Down Expand Up @@ -821,8 +821,8 @@ pg_line_rotate_ip(pgLineObject *self, PyObject *const *args, Py_ssize_t nargs)
pgLineBase *line = &self->line;
double angle, rx, ry;

rx = (line->x1 + line->x2) / 2;
ry = (line->y1 + line->y2) / 2;
rx = (line->xa + line->xb) / 2;
ry = (line->ya + line->yb) / 2;

if (!pg_DoubleFromObj(args[0], &angle)) {
return RAISE(PyExc_TypeError,
Expand Down
16 changes: 8 additions & 8 deletions test/test_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -1520,10 +1520,10 @@ def rotate_point(x, y, rang, cx, cy):
return Line(x1, y1, x2, y2)

def assert_approx_equal(line1, line2, eps=1e-12):
self.assertAlmostEqual(line1.x1, line2.x1, delta=eps)
self.assertAlmostEqual(line1.y1, line2.y1, delta=eps)
self.assertAlmostEqual(line1.x2, line2.x2, delta=eps)
self.assertAlmostEqual(line1.y2, line2.y2, delta=eps)
self.assertAlmostEqual(line1.xa, line2.xa, delta=eps)
self.assertAlmostEqual(line1.ya, line2.ya, delta=eps)
self.assertAlmostEqual(line1.xb, line2.xb, delta=eps)
self.assertAlmostEqual(line1.yb, line2.yb, delta=eps)

l = Line(0, 0, 1, 1)
angles = float_range(-360, 360, 0.5)
Expand Down Expand Up @@ -1555,10 +1555,10 @@ def rotate_point(x, y, rang, cx, cy):
return Line(x1, y1, x2, y2)

def assert_approx_equal(line1, line2, eps=1e-12):
self.assertAlmostEqual(line1.x1, line2.x1, delta=eps)
self.assertAlmostEqual(line1.y1, line2.y1, delta=eps)
self.assertAlmostEqual(line1.x2, line2.x2, delta=eps)
self.assertAlmostEqual(line1.y2, line2.y2, delta=eps)
self.assertAlmostEqual(line1.xa, line2.xa, delta=eps)
self.assertAlmostEqual(line1.ya, line2.ya, delta=eps)
self.assertAlmostEqual(line1.xb, line2.xb, delta=eps)
self.assertAlmostEqual(line1.yb, line2.yb, delta=eps)

l = Line(0, 0, 1, 1)
angles = float_range(-360, 360, 0.5)
Expand Down

0 comments on commit 3c11f8e

Please sign in to comment.