-
-
Notifications
You must be signed in to change notification settings - Fork 164
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
Add more Line
attributes
#3268
Open
itzpr3d4t0r
wants to merge
8
commits into
pygame-community:main
Choose a base branch
from
itzpr3d4t0r:add-more-line-attributes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add more Line
attributes
#3268
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
561b3f2
Adds the following attributes:
itzpr3d4t0r 13d7b87
format
itzpr3d4t0r be08e0b
add missing stub
itzpr3d4t0r 6d10647
fix
itzpr3d4t0r 21f63c5
replace assert-equal with almost-equal
itzpr3d4t0r 67fc8ba
update and fix docs
itzpr3d4t0r adf60cb
Update src_c/doc/geometry_doc.h
itzpr3d4t0r f1d3164
Update docs/reST/ref/geometry.rst
MyreMylar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -347,6 +347,115 @@ pg_line_getlength(pgLineObject *self, void *closure) | |
return PyFloat_FromDouble(pgLine_Length(&self->line)); | ||
} | ||
|
||
static PyObject * | ||
pg_line_get_center(pgLineObject *self, void *closure) | ||
{ | ||
return pg_tuple_couple_from_values_double( | ||
(self->line.ax + self->line.bx) / 2, | ||
(self->line.ay + self->line.by) / 2); | ||
} | ||
|
||
static int | ||
pg_line_set_center(pgLineObject *self, PyObject *value, void *closure) | ||
{ | ||
double m_x, m_y; | ||
DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); | ||
if (!pg_TwoDoublesFromObj(value, &m_x, &m_y)) { | ||
PyErr_SetString( | ||
PyExc_TypeError, | ||
"Invalid center value, expected a sequence of 2 numbers"); | ||
return -1; | ||
} | ||
|
||
double dx = m_x - (self->line.ax + self->line.bx) / 2; | ||
double dy = m_y - (self->line.ay + self->line.by) / 2; | ||
|
||
self->line.ax += dx; | ||
self->line.ay += dy; | ||
self->line.bx += dx; | ||
self->line.by += dy; | ||
|
||
return 0; | ||
} | ||
|
||
static PyObject * | ||
pg_line_get_centerx(pgLineObject *self, void *closure) | ||
{ | ||
return PyFloat_FromDouble((self->line.ax + self->line.bx) / 2); | ||
} | ||
|
||
static int | ||
pg_line_set_centerx(pgLineObject *self, PyObject *value, void *closure) | ||
{ | ||
double m_x; | ||
DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); | ||
if (!pg_DoubleFromObj(value, &m_x)) { | ||
PyErr_SetString(PyExc_TypeError, | ||
"Invalid centerx value, expected a numeric value"); | ||
return -1; | ||
} | ||
|
||
double dx = m_x - (self->line.ax + self->line.bx) / 2; | ||
|
||
self->line.ax += dx; | ||
self->line.bx += dx; | ||
|
||
return 0; | ||
} | ||
|
||
static PyObject * | ||
pg_line_get_centery(pgLineObject *self, void *closure) | ||
{ | ||
return PyFloat_FromDouble((self->line.ay + self->line.by) / 2); | ||
} | ||
|
||
static int | ||
pg_line_set_centery(pgLineObject *self, PyObject *value, void *closure) | ||
{ | ||
double m_y; | ||
DEL_ATTR_NOT_SUPPORTED_CHECK_NO_NAME(value); | ||
if (!pg_DoubleFromObj(value, &m_y)) { | ||
PyErr_SetString(PyExc_TypeError, | ||
"Invalid centery value, expected a numeric value"); | ||
return -1; | ||
} | ||
|
||
double dy = m_y - (self->line.ay + self->line.by) / 2; | ||
|
||
self->line.ay += dy; | ||
self->line.by += dy; | ||
|
||
return 0; | ||
} | ||
|
||
static PyObject * | ||
pg_line_getangle(pgLineObject *self, void *closure) | ||
{ | ||
double dx = self->line.bx - self->line.ax; | ||
|
||
if (dx == 0.0) | ||
return (self->line.by > self->line.ay) ? PyFloat_FromDouble(-90.0) | ||
: PyFloat_FromDouble(90.0); | ||
|
||
double dy = self->line.by - self->line.ay; | ||
double gradient = dy / dx; | ||
|
||
return PyFloat_FromDouble(-RAD_TO_DEG(atan(gradient))); | ||
} | ||
|
||
static PyObject * | ||
pg_line_getslope(pgLineObject *self, void *closure) | ||
{ | ||
double dem = self->line.bx - self->line.ax; | ||
if (dem == 0) { | ||
return PyFloat_FromDouble(0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is inaccurate. The result here should either be an exception or In any case, the vertical line behavior needs to be documented. |
||
} | ||
|
||
double slope = (self->line.by - self->line.ay) / dem; | ||
|
||
return PyFloat_FromDouble(slope); | ||
} | ||
|
||
static PyGetSetDef pg_line_getsets[] = { | ||
{"ax", (getter)pg_line_getax, (setter)pg_line_setax, DOC_LINE_AX, NULL}, | ||
{"ay", (getter)pg_line_getay, (setter)pg_line_setay, DOC_LINE_AY, NULL}, | ||
|
@@ -355,6 +464,14 @@ static PyGetSetDef pg_line_getsets[] = { | |
{"a", (getter)pg_line_geta, (setter)pg_line_seta, DOC_LINE_A, NULL}, | ||
{"b", (getter)pg_line_getb, (setter)pg_line_setb, DOC_LINE_B, NULL}, | ||
{"length", (getter)pg_line_getlength, NULL, DOC_LINE_LENGTH, NULL}, | ||
{"center", (getter)pg_line_get_center, (setter)pg_line_set_center, | ||
DOC_LINE_CENTER, NULL}, | ||
{"centerx", (getter)pg_line_get_centerx, (setter)pg_line_set_centerx, | ||
DOC_LINE_CENTERX, NULL}, | ||
{"centery", (getter)pg_line_get_centery, (setter)pg_line_set_centery, | ||
DOC_LINE_CENTERY, NULL}, | ||
{"angle", (getter)pg_line_getangle, NULL, DOC_LINE_ANGLE, NULL}, | ||
{"slope", (getter)pg_line_getslope, NULL, DOC_LINE_SLOPE, NULL}, | ||
{NULL, 0, NULL, NULL, NULL}}; | ||
|
||
static PyTypeObject pgLine_Type = { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the angle should be negated.
Also it should use some form of
atan2
instead ofatan
, to reach the full 360 deg range, as had been specified in the docs.There's also special cases like
a == b
,inf
, etc., where it maybe should emulate pythonmath.atan2(line.by - line.ay, line.bx - line.ax)
(also see #3222).