Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix line rotate #238

Merged
merged 2 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading