Exemple #1
0
from how_to_think.chapter_11.section_1_12.Point import Point
from how_to_think.chapter_11.section_2_6.Rectangle import Rectangle
from how_to_think.test.test import test_equal

r = Rectangle(Point(0, 0), 10, 5)
test_equal(r.area(), 50)
from how_to_think.chapter_11.section_1_12.Point import Point
from how_to_think.chapter_11.section_2_6.Rectangle import Rectangle
from how_to_think.test.test import test_equal

r = Rectangle(Point(0, 0), 10, 5)
test_equal(r.perimeter(), 30)
from how_to_think.chapter_11.section_1_12.Point import Point
from how_to_think.chapter_11.section_2_6.Rectangle import Rectangle


def test(test_result):
    print("Test has {}".format("succeeded" if test_result else "failed"))


r = Rectangle(Point(0, 0), 10, 5)
test(r.contains(Point(0, 0)))
test(r.contains(Point(3, 3)))
test(not r.contains(Point(3, 7)))
test(not r.contains(Point(3, 5)))
test(r.contains(Point(3, 4.99999)))
test(not r.contains(Point(-3, -3)))
from how_to_think.chapter_11.section_1_12.Point import Point
from how_to_think.chapter_11.section_2_6.Rectangle import Rectangle
from how_to_think.test.test import test_equal

r = Rectangle(Point(0, 0), 10, 5)
test_equal(r.width, 10)
test_equal(r.height, 5)

r.flip()
test_equal(r.width, 5)
test_equal(r.height, 10)
Exemple #5
0
from how_to_think.chapter_11.section_1_12.Point import Point
from how_to_think.chapter_11.section_2_6.Rectangle import Rectangle

r = Rectangle(Point(), 10, 5)
r2 = Rectangle(Point(2, 2), 4, 4)

print(r.collides_with(r2))
print(r2.collides_with(r))

r2.corner = Point(.5, .5)
print(r.collides_with(r2))
print(r2.collides_with(r))

r2.corner = Point(-1, -1)
print(r.collides_with(r2))
print(r2.collides_with(r))

r2.corner = Point(9, -1)
print(r.collides_with(r2))
print(r2.collides_with(r))

r2.corner = Point(11, 0)
print(not r.collides_with(r2))
print(not r2.collides_with(r))

r2.corner = Point(0, -5)
print(not r.collides_with(r2))
print(not r2.collides_with(r))
from how_to_think.chapter_11.section_1_12.Point import Point
from how_to_think.chapter_11.section_2_6.Rectangle import Rectangle

r = Rectangle(Point(100, 50), 10, 5)
print(r.width == 10 and r.height == 5)
r.flip()
print(r.width == 5 and r.height == 10)
Exemple #7
0
from how_to_think.chapter_11.section_1_12.Point import Point
from how_to_think.chapter_11.section_2_6.Rectangle import Rectangle
from how_to_think.test.test import test

player = Rectangle(Point(0, 0), 5, 10)
obstacle = Rectangle(Point(20, 0), 20, 20)

test(not player.collide(obstacle))
player.move(20, 0)
test(player.collide(obstacle))
"""
types of collision:

corners overlapping (each has a corner in the other)
sides overlapping (one has two corners in the other)
fully overlapping (one has four corners in the other)

if any corner is inside the other rectangle there is a collision,
checking all corners of one and opposite corners (e.g. top left en bottom right)
for simplification in the code I checked all corners for both rectangles
if none are in the other there is certainly no collision
"""
from how_to_think.chapter_11.section_1_12.Point import Point
from how_to_think.chapter_11.section_2_6.Rectangle import Rectangle

r = Rectangle(Point(0, 0), 10, 5)
print(r.perimeter() == 30)
Exemple #9
0
from how_to_think.chapter_11.section_1_12.Point import Point
from how_to_think.chapter_11.section_2_6.Rectangle import Rectangle

r = Rectangle(Point(0, 0), 10, 5)
print(r.area() == 50)