예제 #1
0
    def fromFullPoints(cls,
                       points: List[List[Vector2D]],
                       checkpoints: List[List[Vector2D]],
                       startingPoint=Vector2D(0, 0),
                       window=(1920, 1080),
                       monocar: bool = True) -> circuit:
        lines = []
        checkpoint = []
        margin = Vector2D(50, 50)
        allpoints = [item for sublist in points for item in sublist]
        max_x = max([p.x for p in allpoints])
        max_y = max([p.y for p in allpoints])
        scale = min((window[0] - 2 * margin.x) / max_x,
                    (window[1] - 2 * margin.y) / max_y)
        startPoint = startingPoint * scale + margin

        #Create a list of lines from the points
        for k, lane in enumerate(points):
            lines.append([])
            for i in range(len(lane)):
                j = (i + 1) % len(lane)
                lines[k].append(
                    linline.fromPoints(lane[i] * scale + margin,
                                       lane[j] * scale + margin))

        for line in checkpoints:
            l = linline.fromPoints(line[0] * scale + margin,
                                   line[1] * scale + margin)
            l.color = (255, 215, 0)
            checkpoint.append(l)

        return cls(lines,
                   checkpoint,
                   startingPoint=startPoint,
                   monocar=monocar)
예제 #2
0
    def generate(points: list, width: int = 100) -> List[linline]:
        r = width / 2

        lines = []
        directions = []
        #Create a list of lines from the points
        for i in range(len(points)):
            j = (i + 1) % len(points)
            lines.append(linline.fromPoints(points[i], points[j]))
            directions.append(points[j] - points[i])

        print(lines)
        #Create paralel lines
        plines = lines[:]
        paralel_lines = [[], []]
        for i, direction in enumerate(directions):
            normal_vector = direction.toNormalVector() / abs(direction) * r
            # w1 = points[i] + normal_vector
            # w2 = points[i] - normal_vector
            # c1 = lines[i].r @ w1
            # c2 = lines[i].r @ w2
            # paralel_lines[1].append(linline(lines[i].a, lines[i].b, c1))
            # paralel_lines[2].append(linline(lines[i].a, lines[i].b, c2))

            for j in range(2):
                w = points[i] + normal_vector * (-1)**(j + 1)
                c = lines[i].n @ w
                paralel_lines[j].append(linline(lines[i].a, lines[i].b, c))

        print(paralel_lines)
        #Generate joint points
        for ring in paralel_lines:
            intersections = []
            intersections.append(ring[0].intersect(ring[1]))
            for i in range(1, len(ring)):
                j = (i + 1) % len(ring)
                intersection = ring[i].intersect(ring[j])
                print(f"INTERSECTION: {ring[i]} & {ring[j]}: {intersection}")
                intersections.append(intersection)
                if ring[i].b == 0:  #Is line vertical?
                    ring[i].limit = sorted(
                        [intersections[i - 1].y, intersections[i].y])
                else:
                    ring[i].limit = sorted(
                        [intersections[i - 1].x, intersections[i].x])
            if ring[0].b == 0:
                ring[0].limit = sorted(
                    [intersections[0].y, intersections[-1].y])
            else:
                ring[0].limit = sorted(
                    [intersections[0].x, intersections[-1].x])

        #generate
        checkpoints = []
        maximum_length = sum([abs(v) for v in directions])

        vertices = [item for sublist in paralel_lines for item in sublist]
        print(vertices)
        return (vertices, checkpoints)
예제 #3
0
    def generateLines(self):
        lines = []
        eyePoints = [Vector2D(i[0],i[1]) for i in self.eyesList]

        for line in eyePoints:
            secondPosition = self.middle + line.rotate(self.carRotation.rotation())
            lines.append(linline.fromPoints(self.middle, secondPosition))

        self.lines = lines
        return lines
예제 #4
0
    def generateHitbox(self):
        hitbox = []
        lineColor = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (0, 255, 255)]

        rotation = self.carRotation.rotation()

        points = [point.rotate(rotation, in_place=False) + self.middle for point in self.hitboxVectors]
        for i in range(len(points)):
            l = linline.fromPoints(points[i-1], points[i])
            l.color = lineColor[i]
            hitbox.append(l)

        return hitbox
예제 #5
0
    def generateCheckpoints(self, numCheckpoints=10):
        self.numCheckpoints = numCheckpoints
        print(numCheckpoints)
        color = (
            (255, 0, 0),
            (0, 255, 0),
            (0, 0, 255),
            (255, 255, 0),
            (255, 0, 255),
            (0, 255, 255),
        )
        checkpoints = [[], []]

        def appendToCheckpoint(lengthUntilNextCheckpoint, checkpoints, line,
                               currentLengthOfLine,
                               ratioOfLengthToNextCheckpoint):
            beginOfCurrentLine, _ = line.getEndPoints()
            newCheckpoint = beginOfCurrentLine + line.r.normalize(
                in_place=False) * ratioOfLengthToNextCheckpoint
            checkpoints[0].append(newCheckpoint)

            perpindicular = linline.fromVector(line.n, newCheckpoint)
            distance = math.inf
            secondNewCheckpoint = Vector2D(0, 0)
            for outerLine in self.innerLines:
                intersection = perpindicular.intersect(outerLine)
                if intersection is not None:
                    if abs(newCheckpoint - intersection) < distance:
                        distance = abs(newCheckpoint - intersection)
                        secondNewCheckpoint = intersection

            checkpoints[1].append(secondNewCheckpoint)

        lengthOfLine = []
        for line in self.outerLines:
            pointA, pointB = line.getEndPoints()
            lengthOfLine.append(abs(pointA - pointB))

        fullLength = sum(lengthOfLine)
        gapBetweenCheckpoints = fullLength / numCheckpoints
        currentLengthOfLine = lengthOfLine

        untilNextCheckpoint = gapBetweenCheckpoints

        for j, line in enumerate(self.outerLines):
            if (untilNextCheckpoint - currentLengthOfLine[j] > 0):
                print("NEXT")
                untilNextCheckpoint -= currentLengthOfLine[j]
            else:
                ratioOfLengthToNextCheckpoint = untilNextCheckpoint / currentLengthOfLine[
                    j]
                print(ratioOfLengthToNextCheckpoint)
                appendToCheckpoint(untilNextCheckpoint, checkpoints, line,
                                   currentLengthOfLine[j],
                                   ratioOfLengthToNextCheckpoint)

                while (gapBetweenCheckpoints -
                       (1 - ratioOfLengthToNextCheckpoint) *
                       currentLengthOfLine[j] < 0):
                    print("I AM THE CULPRIT")
                    untilNextCheckpoint = gapBetweenCheckpoints + untilNextCheckpoint
                    ratioOfLengthToNextCheckpoint = (
                        untilNextCheckpoint) / currentLengthOfLine[j]
                    appendToCheckpoint(untilNextCheckpoint, checkpoints, line,
                                       currentLengthOfLine[j],
                                       ratioOfLengthToNextCheckpoint)

                untilNextCheckpoint = gapBetweenCheckpoints - (
                    1 - ratioOfLengthToNextCheckpoint) * currentLengthOfLine[j]

        checkpointLines = []
        for i in range(len(checkpoints[0])):
            print(checkpoints[1][i])
            line = linline.fromPoints(checkpoints[0][i], checkpoints[1][i])
            line.color = color[i % len(color)]
            checkpointLines.append(line)

        return checkpointLines
예제 #6
0
    def generate(points: list,
                 width: int = 100,
                 numCheckpoints=10,
                 startingPoint=Vector2D(0, 0),
                 window=[1920, 1080]) -> List[linline]:
        r = width / 2

        lines = []
        directions = []

        #generate scale and apply
        margin = Vector2D(50, 50)
        max_x = max([p.x for p in points])
        max_y = max([p.y for p in points])
        scale = min((window[0] - 2 * margin.x) / max_x,
                    (window[1] - 2 * margin.y) / max_y)
        scaledStartingPoint = startingPoint * scale + margin

        points[:] = [point * scale + margin for point in points]

        #Create a list of lines from the points
        for i in range(len(points)):
            j = (i + 1) % len(points)
            lines.append(linline.fromPoints(points[i], points[j]))
            directions.append(points[j] - points[i])

        print(lines)
        #Create paralel lines
        plines = lines[:]
        paralel_lines = [[], []]
        for i, direction in enumerate(directions):
            normal_vector = direction.toNormalVector() / abs(direction) * r
            # w1 = points[i] + normal_vector
            # w2 = points[i] - normal_vector
            # c1 = lines[i].r @ w1
            # c2 = lines[i].r @ w2
            # paralel_lines[1].append(linline(lines[i].a, lines[i].b, c1))
            # paralel_lines[2].append(linline(lines[i].a, lines[i].b, c2))

            for j in range(2):
                w = points[i] + normal_vector * (-1)**(j + 1)
                c = lines[i].n @ w
                paralel_lines[j].append(linline(lines[i].a, lines[i].b, c))

        print(len(paralel_lines))
        #Generate joint points
        for ring in paralel_lines:
            intersections = []
            intersections.append(ring[0].intersect(ring[1]))
            for i in range(1, len(ring)):
                j = (i + 1) % len(ring)
                intersection = ring[i].intersect(ring[j])
                intersections.append(intersection)
                if ring[i].b == 0:  #Is line vertical?
                    ring[i].limit = [
                        intersections[i - 1].y, intersections[i].y
                    ]
                else:
                    ring[i].limit = [
                        intersections[i - 1].x, intersections[i].x
                    ]
            if ring[0].b == 0:
                ring[0].limit = [intersections[0].y, intersections[-1].y]
            else:
                ring[0].limit = [intersections[0].x, intersections[-1].x]

        #generate
        checkpoints = [
            linline(1, 0, 0)
        ]  # circuit.generateCheckpoints(paralel_lines, numCheckpoints=numCheckpoints)
        maximum_length = sum([abs(v) for v in directions])

        return (paralel_lines, scaledStartingPoint)
예제 #7
0
from classes.improvedLine import linline
from classes.Vector import Vector2D

l3 = linline.fromPoints(Vector2D(1, 1), Vector2D(5, 5))
print(l3.getEndPoints())