在我的程序开始时,我绘制了六个垂直的直线段,然后我随机绘制了用户想要的任意多的线段。每次绘制它时,我都会检查它是否与这六个段之一相交。问题是,即使它们相交,它也永远不会返回 True。
我正在使用 Python,并对两条线使用 y = mx + b 来找到它们相交的公共点,并检查它是否位于两条线段上。这是代码:
import random
import turtle
wn = turtle.Screen()
wn.setworldcoordinates(-50, -50, 50.5, 50)
intersectCount = 0
stickCount = 0
def drawLines():
draw = turtle.Turtle()
for x in range(-5, 6):
if x % 2 != 0:
draw.penup()
draw.goto(x * 10, 50)
draw.pendown()
draw.goto(x * 10, -50)
def drawStick():
draw = turtle.Turtle()
draw.color("Brown")
rand = random.Random()
stickLength = 10 # sticks are 10 units long
x1 = rand.randint(-50, 50)
y1 = rand.randint(-50, 50)
x2 = 0
y2 = 0
direction = rand.randint(1, 4)
if(direction == 1):
x2 = x1 + stickLength
y2 = y1 - stickLength
elif(direction == 2):
x2 = x1 - stickLength
y2 = y1 + stickLength
elif(direction == 3):
x2 = x1 + stickLength
y2 = y1 + stickLength
else:
x2 = x1 - stickLength
y2 = y1 - stickLength
draw.penup()
draw.goto(x1, y1)
draw.pendown()
draw.goto(x2, y2)
global stickCount
stickCount += 1
for x in range(-5, 6):
if x % 2 != 0:
if (checkStick(x * 10, 50, x * 10, -50, x1, y1, x2, y2)):
global intersectCount
intersectCount += 1
break
def drop():
sticks = input("Enter how many sticks you would like to drop: ")
sticks = int(sticks)
for x in range(0, sticks):
drawStick()
print(str(stickCount) + " sticks were dropped")
print("There were " + str(intersectCount) + " sticks that intersected")
def checkStick(x1, y1, x2, y2, sX1, sY1, sX2, sY2):
#greatest and least x coordinates from the line
greatestX = 0
leastX = 0
if(x1 == x2 or x1 > x2):
greatestX = x1
leastX = x2
else:
greatestX = x2
leastX = x1
#same thing but with y's
greatestY = 0
leastY = 0
if(y1 == y2 or y1 > y2):
greatestY = y1
leastY = y2
else:
greatestY = y2
leastY = y1
#same thing but with stick x's
gStickX = 0
lStickX = 0
if(sX1 == sX2 or sX1 > sX2):
greatestX = sX1
leastX = sX2
else:
greatestX = sX2
leastX = sX1
#same thing but with stick x's
gStickY = 0
lStickY = 0
if(sY1 == sY2 or sY1 > sY2):
greatestY = sY1
leastY = sY2
else:
greatestY = sY2
leastY = sY1
#y = mx + b
#the stick
stickSlope = ((sY2 - sY1) / (sX2 - sX1)) # m, or the slope
stickIntercept = sY1 - (stickSlope * sX1) # b = y - (mx)
#the line
lineSlope = 0
if(x2 - x1 != 0): # m, or the slope
lineSlope = (y2 - y1) / (x2 - x1)
lineIntercept = y1 - (lineSlope * x1) # b = y - (mx)
#set the two formulas equal to each other, find x and then y, that is where they intersect#this will be reset as the x of intersection
x = (lineIntercept - stickIntercept) / (stickSlope - lineSlope) # solving for x by getting the x's on one side, and other numbers on one side, then dividing out whatever number x is being multiplied by to get what x is
y = ((stickSlope * x) + stickIntercept) # back to y = mx + b now that we have all the variable to find y
#points intersect at x, y
if(stickSlope == lineSlope):
return False # parallel
else:
#checking if it is within the line segment
if(x <= greatestX and x >= leastX):
if(y <= greatestY and y >= leastY):
#checking if it is within the stick segment
if(x <= gStickX and x >= lStickX):
if(y <= gStickY and x >= lStickY):
return True
else:
return False
else:
return False
else:
return False
else:
return False
drawLines()
drop()
raw_input() # waits for the user to click a key to exit