Cope 2.5.0
My personal "standard library" of all the generally useful code I've written for various projects over the years
Loading...
Searching...
No Matches
point.py
1from .imports import dependsOnPackage
2from typing import Union
3
4#* My Own Point Classes
5@dependsOnPackage('Point', 'Point')
6def findClosestXPoint(target, comparatorList, offsetIndex=0):
7 """ I've forgotten what *exactly* this does. I think it finds the point in a list of
8 points who's x point is closest to the target
9 """
10 finalDist = 1000000
11 result = 0
12
13 # for i in range(len(comparatorList) - offsetIndex):
14 for current in comparatorList:
15 # current = comparatorList[i + offsetIndex]
16 currentDist = abs(target.x - current.x)
17 if currentDist < finalDist:
18 result = current
19 finalDist = currentDist
20
21 return result
22
23@dependsOnPackage('Point', ('Point', 'Pointi', 'Pointf'))
24def getPointsAlongLine(p1, p2):
25 """ I don't remember what this does. """
26 p1 = Pointi(p1)
27 p2 = Pointi(p2)
28
29 returnMe = []
30
31 dx = p2.x - p1.x
32 dy = p2.y - p1.y
33
34 for x in range(p1.x, p2.x):
35 y = p1.y + dy * (x - p1.x) / dx
36 returnMe.append(Pointf(x, y))
37
38 return returnMe
39
40@dependsOnPackage('Point', 'Point')
41def rotatePoint(p, angle, pivotPoint, radians=False):
42 """ This rotates one point around another point a certain amount, and returns it's new position """
43 if not radians:
44 angle = math.radians(angle)
45 # p -= pivotPoint
46 # tmp = pygame.math.Vector2(p.data()).normalize().rotate(amount)
47 # return Pointf(tmp.x, tmp.y) + pivotPoint
48
49 dx = p.x - pivotPoint.x
50 dy = p.y - pivotPoint.y
51 newX = dx * math.cos(angle) - dy * math.sin(angle) + pivotPoint.x
52 newY = dx * math.sin(angle) + dy * math.cos(angle) + pivotPoint.y
53
54 return Pointf(newX, newY)
55
56@dependsOnPackage('Point', 'Point')
57def getMidPoint(p1, p2):
58 """ Returns the halfway point between 2 given points """
59 assert type(p1) == type(p2)
60 # return Pointf((p1.x + p2.x) / 2, (p1.y + p2.y) / 2)
61 return p1._initCopy((p1.x + p2.x) / 2, (p1.y + p2.y) / 2)
62
63@dependsOnPackage('Point', 'Point')
64def findClosestPoint(target, comparatorList):
65 """ Finds the closest point in the list to what it's given"""
66 finalDist = 1000000
67
68 for i in comparatorList:
69 current = getDist(target, i)
70 if current < finalDist:
71 finalDist = current
72
73 return finalDist
74
75@dependsOnPackage('Point', 'Point')
76def collidePoint(topLeft: 'Point', size: Union[tuple, list, 'Size'], target, inclusive=True):
77 """ Returns true if target is within the rectangle given by topLeft and size """
78 return isBetween(target.x, topLeft.x, size[0], beginInclusive=inclusive, endInclusive=inclusive) and \
79 isBetween(target.y, topLeft.y, size[1], beginInclusive=inclusive, endInclusive=inclusive)
80
81@dependsOnPackage('Point', 'Point')
82def getPointDist(a: 'Point', b: 'Point'):
83 return math.sqrt(((b.x - a.x)**2) + ((b.y - a.y)**2))