为什么我会得到更多的交叉点

时间:2019-12-27 22:54:44

标签: python line

我正在尝试制作2d汽车类型的模拟器,我需要检测轨道与汽车的交点。Car program

在我的代码中,它可以检测到这些点以及此处和那里的一些其他随机点。 有人可以帮我弄清楚为什么还有加分吗?

更清楚地说,有6条线从汽车的中心出来。如果这些线中的任何一条接触到轨道的外部(我想先解决问题,然后再进行内部轨道。)我希望乌龟对象“点”转到相交的位置并在此处进行标记。

我正在使用python 3。

我的代码:

import turtle
import math
import numpy as np 
import random
import time


wn = turtle.Screen()
wn.setup(1300, 800)
wn.tracer(0)




car = turtle.Turtle()
car.shape('square')
car.color('red')
car.speed(0)
car.up()
car.shapesize(0.5, 1)
car.goto(0, -200)
wn.update()

#-----------------------------------------------
draw = turtle.Turtle()
draw.speed(0)
draw.pensize(5)
draw.up()

draw.goto(-200, 250)
draw.down()
draw.goto(5, 300)
draw.goto(300, 200)
draw.goto(300, -100)
draw.goto(-100, -300)

draw.goto(-200, 250)

draw.up()

draw.goto(-100, 150)
draw.down()
draw.goto(5, 200)
draw.goto(200, 150)
draw.goto(200, 0)
draw.goto(-50, -100)
draw.goto(-100, 150)
#draw.goto()
draw.up()



#Points = [-200, 250], [5, 300], [5, 300], [300, 200], [300, 200], [300, -100], [300, -100], [-100, -300], [-100, -300], [-200, 250]
#Points = [-100, 150], [5, 200], [5, 200], [200, 150], [200, 150], [200, 0], [200, 0], [-50, -100], [-50, -100], [-100, 150]

#-------------------------------------------------

#point.goto(line_intersection([center, [lines.xcor(), lines.ycor()]], [[-200, 250], [5, 300]]))
#point.goto(line_intersection([center, [lines.xcor(), lines.ycor()]], [[5, 300], [300, 200]]))
#point.goto(line_intersection([center, [lines.xcor(), lines.ycor()]], [[300, 200], [300, -100]]))
#point.goto(line_intersection([center, [lines.xcor(), lines.ycor()]], [[300, -100], [-100, -300]]))
#point.goto(line_intersection([center, [lines.xcor(), lines.ycor()]], [[-100, -300], [-200, 250]]))



wn.update()


lines = turtle.Turtle()
lines.speed(0)
lines.up()
lines.ht()
lines.pensize(3)

point =  turtle.Turtle()
point.up()
point.speed(0)
point.shape('circle')

point.ht()

def draw_lines():
    lines.penup()
    center = [car.xcor(), car.ycor()]
    lines.goto(center)
    lines.setheading(car.heading())
    lines.pendown()
    lines.forward(150)
    lines.penup()
    wn.update()




    line_intersection([center, [lines.xcor(), lines.ycor()]], [[-200, 250], [5, 300]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[5, 300], [300, 200]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[300, 200], [300, -100]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[300, -100], [-100, -300]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[-100, -300], [-200, 250]])
    wn.update()





    lines.goto(center)
    lines.setheading(car.heading())
    lines.pendown()
    lines.backward(150)
    lines.penup()
    wn.update()

    line_intersection([center, [lines.xcor(), lines.ycor()]], [[-200, 250], [5, 300]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[5, 300], [300, 200]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[300, 200], [300, -100]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[300, -100], [-100, -300]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[-100, -300], [-200, 250]])
    wn.update()

    lines.goto(center)
    lines.setheading(car.heading()+45)
    lines.pendown()
    lines.forward(150)  
    lines.penup()
    wn.update()

    line_intersection([center, [lines.xcor(), lines.ycor()]], [[-200, 250], [5, 300]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[5, 300], [300, 200]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[300, 200], [300, -100]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[300, -100], [-100, -300]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[-100, -300], [-200, 250]])
    wn.update()

    lines.goto(center)
    lines.setheading(car.heading()-45)
    lines.pendown()
    lines.forward(150) 
    lines.penup()
    wn.update()

    line_intersection([center, [lines.xcor(), lines.ycor()]], [[-200, 250], [5, 300]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[5, 300], [300, 200]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[300, 200], [300, -100]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[300, -100], [-100, -300]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[-100, -300], [-200, 250]])
    wn.update()


    lines.goto(center)
    lines.setheading(car.heading()-135)
    lines.pendown()
    lines.forward(150)  
    lines.penup()
    wn.update()

    line_intersection([center, [lines.xcor(), lines.ycor()]], [[-200, 250], [5, 300]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[5, 300], [300, 200]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[300, 200], [300, -100]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[300, -100], [-100, -300]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[-100, -300], [-200, 250]])
    wn.update()


    lines.goto(center)
    lines.setheading(car.heading()+135)
    lines.pendown()
    lines.forward(150)  
    lines.penup()
    wn.update()

    line_intersection([center, [lines.xcor(), lines.ycor()]], [[-200, 250], [5, 300]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[5, 300], [300, 200]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[300, 200], [300, -100]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[300, -100], [-100, -300]])
    line_intersection([center, [lines.xcor(), lines.ycor()]], [[-100, -300], [-200, 250]])


    wn.update()
    #lines.clear()


def line_intersection(line1, line2):
    xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
    ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1])

    def det(a, b):
        return a[0] * b[1] - a[1] * b[0]

    div = det(xdiff, ydiff)
    if div == 0:

        return False
    else:

        d = (det(*line1), det(*line2))
        x = det(d, xdiff) / div
        y = det(d, ydiff) / div
        point.goto(x, y)
        point.stamp()







draw_lines()


wn.update()



wn.mainloop()

0 个答案:

没有答案