Skip to content

Make a Point Please!

Bhavye Mathur edited this page Jul 12, 2020 · 4 revisions

The Basics

The Point class is one which is very important and it used while doing almost anything with Goopy. Its primary purpose is to position GraphicsObjects on the GraphWin. Here is how you construct a point:

from goopylib.Point import *  # Only import the Point class
from goopylib.imports import *  # Imports everything

example_point = Point(500, 400)  # Creates a Point with the coordinates (500, 400)

You can use Points while constructing a GraphicsObject like this:

from goopylib.objects.Rectangle import Rectangle

Rectangle(Point(0, 0), Point(100, 300)) # Creates a Rectangle with the coordinates given in the Point

And that's really about all you need to know to do almost everything! But, here are some more useful details that may help you:

# You can access the coordinates of the Point like so
example_point.x
example_point.y
# Points can be unpacked too!
x, y = example_point
# Remember to clone the Point if you want to create a duplicate
new_point = example_point.clone()
# Now let's 'move' the Point
new_point.move(dx=50, dy=20) # move_y, move_x, move_to, move_to_x, move_to_y are also valid
# Want to calculate the distance between 2 points?
example_point.distance(new_point)
# or
example_point.distance_x(new_point) # Only gets the x or y distances
example_point.distance_y(new_point)
# You can even calculate the slope of a line between the 2 points!
example_point.slope(new_point)

Maths with Points

The Point class implements almost all of the mathematical operators in Python and you can perform operations with Points with other Points or with numbers (ints or floats).

print(example_point + 5)  # Adds 5 to both the x & y value, doesn't mutate the Point

example_point /= 10  # Mutates the point
print(example_point) 

When performing operations with integers or floats, the operation is performed on the x & y values individually. When performing an operation with another Point, the x value is operated on by the other x value, and the same for Y.

p = Point(10, 0)
p + 10  # Returns Point(20, 10)

p2 = Point(5, 10)
p + p2  # Returns Point(15, 10)

bool(p < p2)  # Returns True because p is closer to Point(0, 0)

When comparing Points, i.e with <, >, etc. The Point closer to (0, 0) is considered to be lesser.

Here are a few special mentions:

# The << and >> operators when used with ints swap the coordinates those many times
p << 3  # Gives 0, 10
p >> 6  # Gives 10, 0
~p  # Gives Point(p.y, p.x)
5 in p  # Checks if one of the coordinates is 5

The operators defined for the Point are +, -, *, /, //, %, |, &, ^, <<, >>, - as in negating, ==, <, >, <=, >=, +=, -=, *=, /=, %=, //=, |=, &=, ^=, <<=, >>=, ~, in

The builtin functions defined for the Point are dir(), bytes(), hex(), oct(), round(), pow(), bool(), abs(), divmod(), len()

Some Functions not Mentioned

  1. swap() - Swaps the x & y value of the Point
  2. set(x, y), set_x(x), set_y(y) - Sets the x & y value of the Point
  3. get_x(), get_y() - Returns the x & y values
  4. add(dx, dy), add_x(dx), add_y(dy) - Doesn't mutate the point (not recommended, use the + operator)
Clone this wiki locally