One of the essential topics for teaching modeling skills is geometry. Unlike other branches of mathematics, it is visually representable and intuitive for everyone. Let us recall the basic concepts we will deal with. The coordinate plane is the plane that the coordinates system uses.
Coordinates are a set of two lines intersecting at right angles. If the coordinate system is Cartesian, it has called numeric axes:
y |
|
6 |
5 |
4 |
3 | . (2, 3)
2 |
1 |
----------+--------------------
| 1 2 3 4 5 6 x
|
|
We can place a point — the simplest primitive on a plane. We determine its position by two coordinates, written like this:
(2, 3)
The first number is the coordinate on the x
axis, and the second is on the y
axis. In the code, it can be represented as a tuple consisting of two elements:
# x = 2, y = 3
point = (2, 3)
# or
point = 2, 3
It is already enough to perform various operations. For example, to find a symmetric point relative to the x-axis, it is enough to invert the second number — to change the sign to the opposite:
y |
|
6 |
5 |
4 |
3 | . (2, 3)
2 |
1 |
-----------------------------------
-1 | 1 2 3 4 5 6 x
-2 |
-3 | . (2, -3)
-4 |
-5 |
-6 |
Look how to represent it in code:
# x = 2, y = 3
point = 2, 3
x, y = point
# x = 2, y = -3
symmetrical_point = x, -y
Sometimes, we need to find a point between two other points and the middle of the segment. We calculate this point by searching for the arithmetic mean of coordinates. That is, the x
coordinate of the median point is equal to (x1 + x2) / 2
, and the y
coordinate is (y1 + y2) / 2
:
def get_middle_point(p1, p2):
x1, y1 = p1
x2, y2 = p2
x = (x1 + x2) / 2
y = (y1 + y2) / 2
return x, y
point1 = 2, 3
point2 = -4, 1
get_middle_point(point1, point2)
# (-1, 2)
There are a lot of similar operations in geometry. When we speak about code organization, it is logical to put all functions related to the operations with points in the points
module.
In turn, we can combine the points into segments, defining each segment by a pair of points — opposite ends of the segment. Also, we can represent a segment in code similarly to a point in the form of a tuple of two elements:
point1 = 3, 4
point2 = -8, 10
segment = point1, point2
Are there any more questions? Ask them in the Discussion section.
The Hexlet support team or other students will answer you.
For full access to the course you need a professional subscription.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.