def scytheAttack(self, checkList): """ The hit detection if using a SCYTHE. """ self.appPtr.sounds["Scythe"].play() for obj in checkList: ######### HIT DETECTION FOR hitPtFar! ############ if obj == self: # Don't check hits against yourself continue if isinstance(obj, Player) or isinstance(obj, Enemy): # Calculates the distance (scalar) between the hit point and the object distance = math2d.Vector2(obj.pos[0] - self.hitPtFar[0], obj.pos[1] - self.hitPtFar[1] - 10) distance = distance.length() if distance < obj.size + self.hitRadius: # If the object is hit, handle the hit (below) if isinstance(obj, Player): obj.lastHitByPlayer = True self.playHitSound(obj) obj.health -= self.weapon[4] * self.damageScale // 2 for obj in checkList: ######### HIT DETECTION FOR hitPtNear! ########### if obj == self: # Don't check hits against yourself continue if isinstance(obj, Player) or isinstance(obj, Enemy): # Calculates the distance (scalar) between the hit point and the object distance = math2d.Vector2(obj.pos[0] - self.hitPtNear[0], obj.pos[1] - self.hitPtNear[1] - 10) distance = distance.length() if distance < obj.size + self.hitRadius: # If the object is hit, handle the hit (below) if isinstance(obj, Player): obj.lastHitByPlayer = True self.playHitSound(obj) obj.health -= self.weapon[4] * self.damageScale // 2
def test_rotate(self): a = m2d.Vector2(1, 0) b = m2d.Vector2(1, 1) a90 = a.rotate(1.5708) self.assertAlmostEqual(a90.x, 0, places=4) self.assertAlmostEqual(a90.y, 1, places=4) a180 = a90.rotate(1.5708) self.assertAlmostEqual(a180.x, -1, places=4) self.assertAlmostEqual(a180.y, 0, places=4) a270 = a180.rotate(1.5708) self.assertAlmostEqual(a270.x, 0, places=4) self.assertAlmostEqual(a270.y, -1, places=4) a360 = a270.rotate(1.5708) self.assertAlmostEqual(a360.x, 1, places=4) self.assertAlmostEqual(a360.y, 0, places=4) b90 = b.rotate(1.5708) self.assertAlmostEqual(b90.x, -1, places=4) self.assertAlmostEqual(b90.y, 1, places=4) b180 = b90.rotate(1.5708) self.assertAlmostEqual(b180.x, -1, places=4) self.assertAlmostEqual(b180.y, -1, places=4) b270 = b180.rotate(1.5708) self.assertAlmostEqual(b270.x, 1, places=4) self.assertAlmostEqual(b270.y, -1, places=4) b360 = b270.rotate(1.5708) self.assertAlmostEqual(b360.x, 1, places=4) self.assertAlmostEqual(b360.y, 1, places=4)
def test_local_transformation(self): # the division transforms a vector in global coordinates to local coordinates. # vector in local coord = pose3d(robot) in global coordinates / global vector glob_robot_pose = m2d.Pose2D(m2d.Vector2(1000, 0), 0) glob_ball_pose = m2d.Vector2(1200, 0) test = glob_robot_pose / glob_ball_pose self.assertEqual(test.x, 200) self.assertEqual(test.y, 0)
def test_div(self): a = m2d.Vector2(20, -20) b = m2d.Vector2(4, -4) c = m2d.Vector2(-8., 8.) d = a / 5 e = a / -2.5 self.assertEqual(d.x, b.x) self.assertEqual(d.y, b.y) self.assertEqual(e.x, c.x) self.assertEqual(e.y, c.y)
def test_projection(self): begin1 = m2d.Vector2(-1, 0) end1 = m2d.Vector2(1, 0) line1 = m2d.LineSegment(begin1, end1) project1 = line1.projection(m2d.Vector2(0, 1)) project2 = line1.projection(m2d.Vector2(5, 1)) self.assertEqual(project1.x, 0) self.assertEqual(project1.y, 0) self.assertEqual(project2.x, 1) self.assertEqual(project2.y, 0)
def staffP_Attack(self, checkList): if self.drawDirection == 0: projPos = math2d.Vector2(self.pos[0] - 64, self.pos[1] - 20) elif self.drawDirection == 1: projPos = math2d.Vector2(self.pos[0] + 64, self.pos[1] - 20) elif self.drawDirection == 2: projPos = math2d.Vector2(self.pos[0], self.pos[1] - 64) elif self.drawDirection == 3: projPos = math2d.Vector2(self.pos[0], self.pos[1] + 64) newProjectile = Projectile(projPos, "purple projectile", self.spriteDB, self.weapon, self.drawDirection, self, "Power") self.projList.append(newProjectile)
def test_max(self): a1 = m2d.Vector2(0, 0) a2 = m2d.Vector2(1, 1) a = m2d.Rect2d(a1, a2) a_max = a.max() b1 = m2d.Vector2(-1, -1) b2 = m2d.Vector2(1, 1) b = m2d.Rect2d(b1, b2) b_max = b.max() c1 = m2d.Vector2(1, 1) c2 = m2d.Vector2(1, -1) c = m2d.Rect2d(c1, c2) c_max = c.max() d1 = m2d.Vector2(-1, 1) d2 = m2d.Vector2(1, -1) d = m2d.Rect2d(d1, d2) d_max = d.max() self.assertEqual(a_max.x, 1) self.assertEqual(a_max.y, 1) self.assertEqual(b_max.x, 1) self.assertEqual(b_max.y, 1) self.assertEqual(c_max.x, 1) self.assertEqual(c_max.y, 1) self.assertEqual(d_max.x, 1) self.assertEqual(d_max.y, 1)
def test_normalize_length(self): a = m2d.Vector2(1, 0) b = m2d.Vector2(0, -1.) c = m2d.Vector2(math.sqrt(2), math.sqrt(2)) a = a.normalize_length(2.6) b = b.normalize_length(-15) c = c.normalize_length(0) self.assertEqual(a.x, 2.6) self.assertEqual(a.y, 0) self.assertEqual(b.x, 0) self.assertEqual(b.y, 15) self.assertEqual(c.x, 0) self.assertEqual(c.y, 0)
def test_global_transformation(self): # the multiplication transforms a vector in local coordinates to global coordinates. # syntax pose2d(robot) in global coordinates * a vector in the robots coordinate system glob_robot_pose = m2d.Pose2D(m2d.Vector2(-1000, -1000), 0) # test the init method self.assertEqual(glob_robot_pose.translation.x, -1000) self.assertEqual(glob_robot_pose.translation.y, -1000) self.assertEqual(glob_robot_pose.rotation, 0) rel_ball_position = m2d.Vector2(100, 0) glob_ball_pos = glob_robot_pose * rel_ball_position self.assertEqual(glob_ball_pos.x, -900) self.assertEqual(glob_ball_pos.y, -1000)
def test_angle(self): a = m2d.Vector2(1, 0) b = m2d.Vector2(0, -1.) c = m2d.Vector2(math.sqrt(2), math.sqrt(2)) d = m2d.Vector2(-1, 0) a_angle = a.angle() b_angle = b.angle() c_angle = c.angle() d_angle = d.angle() self.assertEqual(a_angle, math.radians(0)) self.assertEqual(b_angle, math.radians(-90)) self.assertEqual(c_angle, math.radians(45)) self.assertEqual(d_angle, math.radians(180))
def chaserMove(self, dT, w): """ AI pattern for enemies that chase you """ chaseDist = 800 for obj in self.visibles: if isinstance(obj, Player): # Calculating the distance between the Player and the Enemy dist = math2d.Vector2(obj.pos[0] - self.pos[0], obj.pos[1] - self.pos[1]) distLength = dist.length( ) # Finding the length of that distance # Start chasing the player if the Player is within the chase distance if distLength <= chaseDist: direct = dist.normalized( ) # Normalizing the distance so that it is a direction only if abs(direct[0]) > abs(direct[1]) and direct[ 0] < 0: # Moving more in the x direction; also moving left self.changeDrawDirection(0) elif abs(direct[0]) > abs(direct[1]) and direct[ 0] > 0: # Moving more in the x direction; also moving right self.changeDrawDirection(1) elif abs(direct[0]) < abs(direct[1]) and direct[ 0] < 0: # Moving more in the y direction; also moving up self.changeDrawDirection(2) elif abs(direct[0]) < abs(direct[1]) and direct[ 0] > 0: # Moving more in the y direction; also moving down self.changeDrawDirection(3) # Making sure it doesn't walk through walls #ignoring because GHOSTS ARE HOMING if (w.isSpotWalkable((self.pos[0]) + direct[0] * (self.size + self.tempSpeed * dT), (self.pos[1]))): self.pos[0] += direct[0] * self.tempSpeed * dT #if (w.isSpotWalkable((self.pos[0]), (self.pos[1]) + direct[1] * (self.size + self.tempSpeed * dT))): self.pos[1] += direct[1] * self.tempSpeed * dT break # After chasing one object, don't chase another
def swordP_Attack(self, checkList): for obj in checkList: if obj == self: # Don't check hits against yourself continue if isinstance(obj, Player) or isinstance(obj, Enemy): if self.drawDirection == 0: # Facing West if obj.pos[0] > self.pos[ 0]: # If the object is to your right, skip it continue elif self.drawDirection == 1: # Facing East if obj.pos[0] < self.pos[ 0]: # If the object is to your left, skip it continue elif self.drawDirection == 2: # Facing North if obj.pos[1] > self.pos[ 1]: # If the object is below you, skip it continue elif self.drawDirection == 3: # Facing South if obj.pos[1] < self.pos[ 1]: # If the object is above you, skip it continue # Calculates the distance (scalar) between the hit point and the object distance = math2d.Vector2(obj.pos[0] - self.pos[0], obj.pos[1] - self.pos[1]) distance = distance.length() # 96 is the distance the Scythe reaches (192/2) if distance < 96: # If the object is hit, handle the hit (below) if isinstance(obj, Player): obj.lastHitByPlayer = True self.playHitSound(obj) obj.health -= self.weapon[5] * self.damageScale
def staffAttack(self, checkList): """ Hit detection for the STAFF. Creates a projectile that handles all hit detection """ ####### Finding the position of the new projectile ####### self.appPtr.sounds["staff"].play() if self.drawDirection == 0: projPos = math2d.Vector2(self.pos[0] - 64, self.pos[1] - 20) elif self.drawDirection == 1: projPos = math2d.Vector2(self.pos[0] + 64, self.pos[1] - 20) elif self.drawDirection == 2: projPos = math2d.Vector2(self.pos[0], self.pos[1] - 64) elif self.drawDirection == 3: projPos = math2d.Vector2(self.pos[0], self.pos[1] + 64) newProjectile = Projectile(projPos, "Blue projectile", self.spriteDB, self.weapon, self.drawDirection, self, "Normal") self.projList.append(newProjectile)
def rangeFire(self, dT, w): """AI to fire a projectile in a random direction""" if self.drawDirection == 0: projPos = math2d.Vector2(self.pos[0] - 64, self.pos[1] - 20) elif self.drawDirection == 1: projPos = math2d.Vector2(self.pos[0] + 64, self.pos[1] - 20) elif self.drawDirection == 2: projPos = math2d.Vector2(self.pos[0], self.pos[1] - 64) elif self.drawDirection == 3: projPos = math2d.Vector2(self.pos[0], self.pos[1] + 64) try: if projPos: # Debugging newProjectile = Projectile(projPos, "arrow", self.spriteDB, self.weapon, self.drawDirection, self) self.projList.append(newProjectile) except: pass
def __init__(self): """ Initializes all data """ self.quit = False self.pygameStartup() self.soundeffects = soundDataBase.SoundDBase("sounds\\sound effects") self.GManager = gui_manager.GUI_Manager(self) # Temporarily create a single player (keyboard) & pane self.IDeviceMasterList = [idevice.Keyboard()] numSticks = pygame.joystick.get_count() count = 0 while numSticks > 0: self.IDeviceMasterList.append(idevice.Gamepad(count)) numSticks -= 1 count += 1 self.spriteSheets = spriteDataBase.ImageDBase("imgs\\player") self.spriteSheets.addAdditionalDirectory("imgs\\enemy") self.spriteSheets.addAdditionalDirectory("imgs\\pickup items") #self.spriteSheets = spriteDataBase.ImageDBase("..\\Art") # Designate a path to your spritesheets folder. #self.spriteSheets.addAdditionalDirectory("..\\Art\\Enemies") #self.spriteSheets.addAdditionalDirectory("..\\Art\\Pick up items") self.tileDBase = spriteDataBase.ImageDBase("imgs\\floor") self.tileDBase.addAdditionalDirectory("imgs\\wall") self.guiDBase = spriteDataBase.ImageDBase("imgs\\gui") #SOUND CODE... self.soundeffects = soundDataBase.SoundDBase("sounds\\sound effects") self.music = soundDataBase.SoundDBase("sounds\\music") self.songs = 6 #How many fight themes are in the music folder... self.musicOrder = [ random.randint(1, self.songs), ] #self.musicOrder will have randomized numbers from one to however many songs there are. #Songs are looped in this order, in-game. The following code randomly appends the next numbers... newval = None numberUnused = False counter = 1 while counter < self.songs: #There are six songs so far. val = random.randint(1, self.songs) for i in self.musicOrder: if val == i: numberUnused = False break else: numberUnused = True if numberUnused: self.musicOrder.append(val) counter += 1 self.musicIndex = 0 #This index is incremented in self.update() #self.world = world.World((6,4), self.tileDBase, self.spriteSheets) self.panes = [] # TEMPORARY -- just to test idevice code. self.testPos = math2d.Vector2(400, 300)
def test_rotate_right(self): a = m2d.Vector2(1, 0) b = m2d.Vector2(0, -1.) c = m2d.Vector2(math.sqrt(2), math.sqrt(2)) a = a.rotate_right() b = b.rotate_right() c = c.rotate_right() self.assertEqual(a.x, 0) self.assertEqual(a.y, -1) self.assertEqual(b.x, -1) self.assertEqual(b.y, 0) self.assertEqual(c.x, math.sqrt(2)) self.assertEqual(c.y, -math.sqrt(2)) a = a.rotate_right().rotate_right() self.assertEqual(a.x, 0) self.assertEqual(a.y, 1)
def test_mul(self): a = m2d.Vector2(50, -50) b = m2d.Vector2(1, 0) c = m2d.Vector2(0, 1) d = m2d.Vector2(0.5, 7) # Scalarproduct ab = a * b ac = a * c ad = a * d self.assertEqual(ab, 50) self.assertEqual(ac, -50) self.assertEqual(ad, -325) e = 0.5 f = 0 g = -2 ea = e * a # Test for __rmul__ ae = a * e fa = a * f ga = a * g self.assertEqual(ea.x, 25) self.assertEqual(ea.y, -25) self.assertEqual(ae.x, 25) self.assertEqual(ae.y, -25) self.assertEqual(fa.x, 0) self.assertEqual(fa.y, 0) self.assertEqual(ga.x, -100) self.assertEqual(ga.y, 100) # Negative Test with self.assertRaises(TypeError): t = m2d.Vector2(50, -50) z = m3d.Vector3(1, 0, 1) t * z
def test_point(self): begin1 = m2d.Vector2(1, 0) end1 = m2d.Vector2(1, 1) begin2 = m2d.Vector2(-1, -1) end2 = m2d.Vector2(2, 2) begin3 = m2d.Vector2(1, 1) end3 = m2d.Vector2(-1, 1) line1 = m2d.LineSegment(begin1, end1) line2 = m2d.LineSegment(begin2, end2) line3 = m2d.LineSegment(begin3, end3) line_point1 = line1.point(20) line_point2 = line1.point(0.5) line_point3 = line2.point(3) line_point4 = line3.point(1) self.assertEqual(line_point1.x, 1) # equal to end point self.assertEqual(line_point1.y, 1) self.assertEqual(line_point2.x, 1) self.assertEqual(line_point2.y, 0.5) self.assertAlmostEquals(line_point3.x, 3 / math.sqrt(2) - 1) self.assertAlmostEquals(line_point3.y, 3 / math.sqrt(2) - 1) self.assertEqual(line_point4.x, 0) self.assertEqual(line_point4.y, 1)
def test_add(self): a = m2d.Vector2(100, 100) b = m2d.Vector2(1000, 10) c = m2d.Vector2(-100, -100) d = m2d.Vector2(-5000, 5.3) e = m2d.Vector2(0.245, -32.28) ab = a + b self.assertEqual(ab.x, 1100) self.assertEqual(ab.y, 110) ac = a + c self.assertEqual(ac.x, 0) self.assertEqual(ac.y, 0) de = d + e self.assertEqual(de.x, -4999.755) self.assertEqual(de.y, -26.98) cc = c + c self.assertEqual(cc.x, -200) self.assertEqual(cc.y, -200)
def test_invert(self): glob_robot_pose = m2d.Pose2D(m2d.Vector2(-1000, -1000), 20) test = ~glob_robot_pose testtest = ~test self.assertAlmostEqual(glob_robot_pose.translation.x, testtest.translation.x, places=9) self.assertAlmostEqual(glob_robot_pose.translation.y, testtest.translation.y, places=9) self.assertAlmostEqual(glob_robot_pose.rotation, testtest.rotation, places=9)
def test_normalize(self): a = m2d.Vector2(1, 0) b = m2d.Vector2(0, -1.) c = m2d.Vector2(math.sqrt(2), math.sqrt(2)) d = m2d.Vector2(0, 0) a = a.normalize() b = b.normalize() c = c.normalize() d = d.normalize() self.assertEqual(a.x, 1) self.assertEqual(a.y, 0) self.assertEqual(b.x, 0) self.assertEqual(b.y, -1.) self.assertAlmostEqual(c.x, math.sqrt(2) / 2) self.assertAlmostEqual(c.y, math.sqrt(2) / 2) self.assertEqual(d.x, 0) self.assertEqual(d.y, 0) c_n = c.normalize() self.assertEqual(c_n.x, c.x) self.assertEqual(c_n.y, c.y)
def __init__(self, pos, spriteName, spriteDBase): self.state = 0 # When 0, alive. When 1, dying. When 2, it is dead, and needs to be removed self.spriteDB = spriteDBase # Needed for creating projectiles self.sprite = spriteDBase.get(spriteName) if not isinstance( pos, math2d.Vector2 ): # Making sure the position of this Entity is a Vector2 object self.pos = math2d.Vector2( pos[0], pos[1] ) # WORLD position of the bottom-middle of the character (feet when standing) else: self.pos = pos self.size = 20 # Radius of the bounding circle (centered at self.pos) used for wall / trap hit-detection # Normal size of (nearly) all entities is set here self.hitRadius = 5 # The radius of the circle used for weapons self.damageScale = 25 # Multiplied by the weapon damage factor
def scytheP_Attack(self, checkList): for obj in checkList: if obj == self: # Don't check hits against yourself continue if isinstance(obj, Player) or isinstance(obj, Enemy): # Calculates the distance (scalar) between the hit point and the object distance = math2d.Vector2(obj.pos[0] - self.pos[0], obj.pos[1] - self.pos[1]) distance = distance.length() # 96 is the distance the Scythe reaches (192/2) if distance < 96: # If the object is hit, handle the hit (below) if isinstance(obj, Player): obj.lastHitByPlayer = True self.playHitSound(obj) obj.health -= self.weapon[5] * self.damageScale
def __init__(self, inputObject, worldPos, moverSpriteDBase, dimensions, world, appPtr, paneScreenPos, paneNum): self.appPtr = appPtr self.world = world self.paneNum = paneNum self.idevice = inputObject # Something derived from # idevice.IDevice self.surface = pygame.Surface(dimensions) self.cameraPos = worldPos - math2d.Vector2(dimensions[0] / 2, dimensions[1] / 2) self.player = entity.Player(worldPos, "Human", moverSpriteDBase, appPtr.soundeffects) self.visibleObjects = [] self.traps = [] self.trapCheckList = [self.player] #self.GManager = gui_manager.GUI_Manager() for enemy in world.enemies: self.trapCheckList.append(enemy) self.paneScreenPos = paneScreenPos
def respawn(self, world): spawnPoints = copy.deepcopy( BigDict.BigDict["WorldObjects"]["Spawnpoints"]) self.state = 0 # Revert the state of the player to 0 (normal) self.health = 100 # Return the player to full health spawnIndex = random.randint(0, len(spawnPoints) - 1) self.pos = spawnPoints[ spawnIndex] # Put the player's position at the chosen spawnPoint (randomly picked from possible spawnPoints) self.pos = math2d.Vector2( self.pos[0], self.pos[1]) # To make sure position is a vector, not a tuple if not world.isSpotWalkable(self.pos[0], self.pos[1], "normal") or world.isSpotTrap( self.pos[0], self.pos[1] - 5) != -1: print("Trying respawn again, you got stuck in a wall or trap!") self.respawn(world) self.weapon = BigDict.BigDict["Pickups"]["Weapons"]["Melee"][ 0] # Sets self.weapon to the Sword list self.weaponSprite = self.spriteDB.get(self.weapon[0]) self.sprite = self.spriteDB.get("Human") self.name = "Human"
def update(self, dT, checkList, world): """ Updates the projectile. Performs the following: 1: Move the projectile in the correct direction 2: Determine how far the projectile has travelled - If it has travelled the maximum range, change its state to 2 (dead) 3: Check to see if the projectile hits anything """ displacement = self.speed * dT # This is the amount of distance the projectile has moved ####### Move the projectile ####### if self.direction == 0: # West self.pos[0] -= displacement elif self.direction == 1: # East self.pos[0] += displacement elif self.direction == 2: # North self.pos[1] -= displacement elif self.direction == 3: # South self.pos[1] += displacement self.distTravelled += displacement # Add to the total distance travelled by the projectile if self.distTravelled >= self.range: # The projectile has moved its maximum range self.state = 2 # If this gets code stomped again, someone dies if not (world.isSpotWalkable((self.pos[0]), (self.pos[1]))): self.state = 2 for obj in checkList: # Does the actual hit detection for this projectile if obj == self.shooter: # Don't hit yourself continue if isinstance(obj, Player) or isinstance(obj, Enemy): # Calculates the distance (scalar) between the hit point and the object distance = math2d.Vector2(obj.pos[0] - self.pos[0], obj.pos[1] - self.pos[1] - 10) distance = distance.length() if distance < obj.size + self.hitRadius: # If the object is hit, handle the hit (below) if isinstance(obj, Player): obj.lastHitByPlayer = True obj.health -= self.damage * self.damageScale self.state = 2
def test_intersect(self): begin1 = m2d.Vector2(1, 0) end1 = m2d.Vector2(1, 1) begin2 = m2d.Vector2(-1, -1) end2 = m2d.Vector2(2, 2) begin3 = m2d.Vector2(1, 1) end3 = m2d.Vector2(-1, 1) begin4 = m2d.Vector2(-1, 1) end4 = m2d.Vector2(-1, -1) line1 = m2d.LineSegment(begin1, end1) line2 = m2d.LineSegment(begin2, end2) line3 = m2d.LineSegment(begin3, end3) line4 = m2d.LineSegment(begin4, end4) intersect1 = line1.intersect(line2) intersect2 = line2.intersect(line3) intersect3 = line3.intersect(line1) intersect4 = line4.intersect(line1) self.assertEqual(intersect1, 1) self.assertEqual(intersect2, 1) self.assertEqual(intersect3, 1) self.assertEqual(intersect4, 0)
def populate(self): """ Scans the map and creates Enemy, Trap, Loot objects """ # Do Traps to start with, then eventually add the other type of "things" BigDict.BigDict["WorldObjects"]["Spawnpoints"] = [] self.enemies = [] self.chests = [] self.items = [] tempdict = BigDict.BigDict # Scans through entire map code and documents the entities position in pixels for line in range(len(self.Map)): for var in range(len(self.Map[line])): tmp = self.Map[line][var][0] tmp2 = self.Map[line][var][1:] pos = [] entityX = var * 32 entityY = line * 32 pos = math2d.Vector2(entityX, entityY) self.spawnEnemies if tmp == "c": pass #self.chests.append(entity.chests(pos, "chest", self.spriteDBase)) elif tmp == "s": if line < 32: #Top bar of map BigDict.BigDict["WorldObjects"]["Spawnpoints"].append( pos) elif line > ((self.dim[1] - 1) * 32): #Bottom bar of map BigDict.BigDict["WorldObjects"]["Spawnpoints"].append( pos) elif var < 32: #Left bar of map BigDict.BigDict["WorldObjects"]["Spawnpoints"].append( pos) elif var > ((self.dim[0] - 1) * 32): #Right bar of map BigDict.BigDict["WorldObjects"]["Spawnpoints"].append( pos)
def test_line_intersection(self): begin1 = m2d.Vector2(1, 0) end1 = m2d.Vector2(1, 1) begin2 = m2d.Vector2(-1, -1) end2 = m2d.Vector2(2, 2) begin3 = m2d.Vector2(1, 1) end3 = m2d.Vector2(-1, 1) line1 = m2d.LineSegment(begin1, end1) line2 = m2d.LineSegment(begin2, end2) line3 = m2d.LineSegment(begin3, end3) line_intersection1 = line1.line_intersection(line2) line_intersection2 = line2.line_intersection(line3) line_intersection3 = line3.line_intersection(line1) self.assertEqual(line_intersection1, 1) self.assertAlmostEquals(line_intersection2, math.sqrt(8)) self.assertEqual(line_intersection3, 0)
def test_end(self): begin1 = m2d.Vector2(1, 0) end1 = m2d.Vector2(1, 1) begin2 = m2d.Vector2(-1, -1) end2 = m2d.Vector2(2, 2) begin3 = m2d.Vector2(1, 1) end3 = m2d.Vector2(-1, 1) line1 = m2d.LineSegment(begin1, end1) line2 = m2d.LineSegment(begin2, end2) line3 = m2d.LineSegment(begin3, end3) line_end1 = line1.end() line_end2 = line2.end() line_end3 = line3.end() self.assertEqual(line_end1.x, 1) self.assertEqual(line_end1.y, 1) self.assertEqual(line_end2.x, 2) self.assertEqual(line_end2.y, 2) self.assertEqual(line_end3.x, -1) self.assertEqual(line_end3.y, 1)