python find vector direction between two 3D points - python

I'm trying to calculate the direction of a 3D vector starting at point (x, y, z) and ending at point (a, b, c) for the navigation in my spaceship game, but I have been unable to find anything helpful. So far I have tried using two circles, one for figure out x and y and another for z, to figure it out and the code only works if the two vector's distances are very similar.
Here is what I'm using:
def setcourse(self, destination):
x1, y1, z1 = self.coords
x2, y2, z2 = destination
dx = x2 - x1
dy = y2 - y1
dz = z2 - z1
self.heading = math.atan2(dy, dx)
self.heading2 = math.atan2(dz, dy)
self.distance = int(math.sqrt((dx) ** 2 + (dy) ** 2))
self.distance2 = int(math.sqrt((dy) ** 2 + (dz) ** 2))
def move(self):
if self.distance > 0 and self.distance2 > 0:
if self.atwarp == True:
x, y, z = self.coords
x += math.cos(self.heading) * self.speed
y += math.sin(self.heading) * self.speed
z += math.sin(self.heading2) * self.speed
self.coords = (x, y, z)
print(str(self.coords))
self.distance -= self.speed
self.distance2 -= self.speed
elif self.distance <= 0 and self.distance2 <= 0 and self.atwarp == True:
self.atwarp = False
self.speed = 0
print("Reached Destination")
else:
self.atwarp = False
I'm not sure how much of it is a math error and how much is a programming one, but the z winds up way off and I'm not sure how to go about fixing it. No matter what I do the z is always off if its input more than slightly different from the others.
Here is examples starting from (0, 0, 0). I'm trying to get the output to be similar if not the same as the input.
Input: (100, 200, -200)
Vector1 heading in radians: 1.1071487177940904
Vector2 heading: 2.356194490192345
Vector1 distance: 223
Vector2 distance: 282
Output: (99.7286317964909, 199.4572635929818, 157.68481220460077)
The x and y are fine, but the z is off.
Input: (-235, 634, -21)
Vector1 heading in radians: 1.9257588105240444
Vector2 heading: 1.6039072496758664
Vector1 distance: 676
Vector2 distance: 634
Output: (-220.3499891866359, 594.4761410396925, 633.6524941214135)
The z off.

The direction of the movement is the trio dx, dy, dz you calculated. This vector is not pure:
it contains distance and direction. If you want direction alone, you have to normalize
this:
The distance is sqrt(dx^2 + dy^2 + dz^2).
For the normalized direction, you divide each dx, dy, and dz by this number.
If you want to move in that direction, the new position is the old position plus
the the direction vector times the distance you want to travel:
newpos = oldpos + dist * dirvector
I'm not sure what you mean by input: (100, 200, -200) if that is the direction,
your direction vector would be 300 long, and the actual direction vector is
100/300, 200/300, and -200/300 (so 0.333, 0.667 and -0.667)
If you want to travel 500 along that direction, the new position is
0+166.67, 0+333.33, and 0-333.33

Related

How do I get the minimum distance between a point and a parabola using Python?

What I want to make is angrybirds game.
There is a requirement
1.Draw a rectangle randomly between 100 and 200 in length and length 10 in length.
2. Receive the user inputting the launch speed and the launch angle.
3. Project shells square from origin (0,0).
4. If the shell is hit, we'll end it, or we'll continue from number two.
So this is what I wrote
import turtle as t
import math
import random
def square():
for i in range(4):
t.forward(10)
t.left(90)
def fire():
x = 0
y = 0
speed = int(input("속도:"))
angle = int(input("각도:"))
vx = speed * math.cos(angle * 3.14/180.0)
vy = speed * math.sin(angle * 3.14/180.0)
while t.ycor() >= 0:
vx = vx
vy = vy - 10
x = x + vx
y = y + by
t.goto(x,y)
d = t.distance(d1+5, 5)
if d < 10:
print("Game End")
else:
t.up()
t.goto(0,0)
t.down()
fire()
d1 = random.randint(100,200)
t.up()
t.forward(d1)
t.down()
square()
t.up()
t.goto(0,0)
t.down()
fire()
I want to get an answer to this problem.
The problem is I want to calculate a minimum distance between a point(target point is (d1+5,5)) and a parabola which turtle draw. so I try to find an answer searching in google, python book, but I can't find it.
please help me
You can use basic calculus to determine this minimum distance.
Let's suppose you have point (x1, y1) and parabola y = ax^2 + bx + c
Distance between point and parabola at specific x let's name d(x)
d(x) = sqrt((x-x1)^2 + (ax^2 + bx + c - y1)^2)
We can find local minimum by calculating derivative and equate to 0 (we can ignore squared root and 2 - increasing functions won't change minimum):
x-x1 + (b + 2ax)(c + bx + ax^2 - y1) = 0
This is cubic equation, which you can solve analytically (https://en.wikipedia.org/wiki/Cubic_equation) or numerically (here you can use numpy, https://docs.scipy.org/doc/numpy/reference/generated/numpy.roots.html)
Those 2 methods will give you x coordinate of point on parable minimizing the distance. Now you just have to calculate it, using d(x) formula.
There's a bug in your game: the fire's y coordinate often goes way below 0, making it hard to end up with the box.
import turtle as t
from math import sin,cos,pi
import random
def square():
for i in range(4):
t.forward(10)
t.left(90)
def fire():
x = 0
y = 0
speed = int(input("속도:"))
angle = int(input("각도:"))
vx = speed *cos(angle * pi/180.0)
vy = speed * sin(angle * pi/180.0)
while True:
vx = vx
vy = vy - 10
x = x + vx
if y + vy >= -10:
y = y + vy
t.goto(x,y)
else:
break
d = t.distance(d1+5, 5)
print(d)
if d < 10:
print("Game End")
else:
t.up()
t.home()
t.down()
fire()
d1 = random.randint(100,200)
t.up()
t.forward(d1)
t.down()
square()
t.up()
t.home()
t.down()
fire()

How to plot circles every 20 pixels between two randomly generated points in Pygame?

The homework question I'm working on is the following:
"Draw two randomly placed radius 10 circles on the screen then draw radius 2 circles every twenty pixels from the center of one to the center of the other."
I'm having no trouble randomly generating the two radius 10 circles, but I have no idea how to plot the radius 2 circles between them.
I've tried briefly to plot a line between them, and if there's a way to plot my points along that line, I could definitely do that. I've looked up similar issues and a lot of them mention Bresenham's line algorithm but I doubt that is the answer as it seems very advanced.
Here is the code I have so far for the problem:
import pygame
from random import randint
linecolour = 0,0,0
bgcolour = 255, 255, 255
width = 600
height = 600
screen = pygame.display.set_mode((width, height))
running = 1
x = []
y = []
for i in range (2):
x.append(randint(0,600))
y.append(randint(0,600))
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT: # or other types of events
done = True
screen.fill(bgcolour)
for i,j in zip(x,y):
pygame.draw.circle(screen,linecolour,(i,j),10)
pygame.draw.circle(screen,linecolour,(i,j),2)
pygame.display.flip()
Calculate the direction vector from 1 point to the other:
dir = x[1]-x[0], y[1]-y[0]
Calculate the Euclidean distance between the points. Note you've to import math:
dist = math.sqrt(dir[0]*dir[0] + dir[1]*dir[1])
or as pointed out in the answer of #MadPhysicist
dist = math.hypot(*dir)
the number of points to be drawn is int(dist) // 20. Calculate the points on the line in a loop:
for i in range(int(dist) // 20):
px = int(x[0] + dir[0] * i*20/dist)
py = int(y[0] + dir[1] * i*20/dist)
The code to draw the small points may look like this:
done = False
while not done:
# [...]
dir = x[1]-x[0], y[1]-y[0]
dist = math.hypot(*dir)
for i in range(1, int(dist) // 20 + 1):
pt = int(x[0] + dir[0] * i*20/dist), int(y[0] + dir[1] *i*20/dist)
pygame.draw.circle(screen, linecolour, pt, 2)
This is a very simple problem if you look at it the right way. I would recommend looking at it in terms of polar coordinates. If you have two circles centered at (x[0], y[0]) and (x[1], y[1]), the slope of the line between them is (y[1] - y[0]) / (x[1] - x[0]), but you can also look at the angle of the line:
phi = math.atan2(y[0] - y[1], x[0] - x[1])
The distance from one center to the other is given by
r = math.hypot(y[0] - y[1], x[0] - x[1])
Now you can easily step along the line from (x[0], y[0]) at an angle of phi in steps of 20 until your distance exceeds r. The x-coorindate of the i-th step will be
i * 20 * math.cos(phi)
Similarly the y-coordinate will be
i * 20 * math.sin(phi)
You can calculate the total number of steps as r // 20. Also, math.sin(math.atan2(y, x)) simplifies to y / math.hypot(y, x) and the similar cosine simplifies to x / math.hypot(y, x). So all in all, you get
sep = 20
dx = x[1] - x[0]
dy = y[1] - y[0]
r = math.hypot(dy, dx)
n = int(r // sep)
x_step = sep * dx / r
y_step = sep * dy / r
coords = [(x[0] + i * x_step, y[0] + i * y_step) for i in range(n)]
If you need integer coordinates:
coords = [(x[0] + int(i * x_step), y[0] + int(i * y_step)) for i in range(n)]
To plot:
for coord in [(x[0] + int(i * x_step), y[0] + int(i * y_step)) for i in range(n)]:
pygame.draw.circle(screen, linecolour, coord, 2)

How to calculate the distance from a given point on the surface of a square to its edge with any given direction?

I am trying to figure out what the distance is from a point to the edge of a square's surface (regardless of its angle of direction). I've attached a rough drawing of what I'm talking about.
The grid is centered around the square (although poorly drawn). The distance from the center of the square to the center of the circle. I was hoping to find a way to calculate the distance to the edge of the square from the circle no matter which direction it is headed without having to use a lot of if-else statements in my code.
Let me know if you have any nice ideas!
As far as I understand, you define coordinates and direction and want to find intersection edge point. Make equations for moving along both coordinates and calculate the first time of intersection. There is no magic way without if's
vx = Cos(Direction)
vy = Sin(Direction)
x = x0 + vx * t
y = y0 + vy * t
//potential border positions
if vx > 0 then
ex = x2
else
ex = x1
if vy > 0 then
ey = y2
else
ey = y1
//check for horizontal/vertical directions
if vx = 0 then
return cx = x0, cy = ey
if vy = 0 then
return cx = ex, cy = y0
//in general case find times of intersections with horizontal and vertical edge line
tx = (ex - x0) / vx
ty = (ey - y0) / vy
//and get intersection for smaller parameter value
if tx <= ty then
return cx = ex, cy = y0 + tx * vy
else
return cx = x0 + ty * vx, cy = ey

How to generate squares (randomly located, equally sized, randomly rotated) that don't intersect each other?

I've been working on generating a layer of randomly rotated and placed squares on a 1x1 grid. I have been able to generate a single square that is randomly placed and rotated on the grid, but I'm not sure how to improve the code to generate more random squares that do not intersect with each other. Current code seen below:
Example of my One Randomized Square
from math import cos, pi, sin
from random import randint
from matplotlib.mlab import frange
from matplotlib.pyplot import plot, axis, show
def flake_position_layer1(): #Determines the initial position of one corner of the square
x0 = randint(0, 100) / 100
y0 = randint(0, 100) / 100
theta = randint(0, 90) * pi / 180 #Angle of rotation for the square
return x0, y0, theta
def flake_shape(): #generates the other 3 corners of the square
x0, y0, z, theta = flake_position_layer1()
x1 = x0 + (0.1 * cos(theta))
x2 = x1 + (0.1 * cos((90 * pi/180) + theta))
x3 = x2 + (0.1 * cos((180 * pi/180) + theta))
y1 = y0 + (0.1 * sin(theta))
y2 = y1 + (0.1 * sin((90 * pi/180) + theta))
y3 = y2 + (0.1 * sin((180 * pi/180) + theta))
return x0, x1, x2, x3, y0, y1, y2, y3
def display(): #connects the 4 corners on a plot
x0, x1, x2, x3, y0, y1, y2, y3 = flake_shape()
return plot([x0, x1, x2, x3, x0], [y0, y1, y2, y3, y0])
display()
axis([0,1,0,1]) #1x1 grid
show()
I do not have a CS background (I'm an environmental engineering major) and I am extremely inexperienced with coding. Please give me any recommendations that you may have for me to try and tackle this problem with!
Math background
1. Algebra
1st degree function (or [Wikipedia]: Linear function) is a function ([Wikipedia]: function) whose:
Expression can be written as: f(x) = a * x + b (a and b constants, x variable)
Graphical representation is a straight line in the xOy plane ([Wikipedia]: Cartesian coordinate system)
2. Plane Geometry
A plane consists of an (infinite) number of points: let's refer to a point by its coordinates which can be referred to as:
abscissa or horizontal coordinate or (simply) x
ordinate or vertical coordinate or (simply) y
The points in the plane spread across 2 dimensions
In the plane, every point can be uniquely identified by its x and y
Some of the points in the plane might have some common characteristics: e.g. a bunch of points that are on a straight line... a point that is on a straight line satisfies the line straight line equation (which is an expression generally defined as ${function (from previous paragraph) result} = ${value})
So, in short: for a point P0(x0, y0), if y0 == f(x0), the point is located on that straight line (and more: depending on y0 being greater / lower than f(x0), P0 is located above / below the straight line in the xOy plane). Again, I want to state that for non linear functions, y = f(x) still applies (as it's the general equation formula), but other operators (e.g. <, >) don't
! Important Note !: everything discussed here applies to a variety of functions (don't want to say all), but I'm limiting to linear functions, for clarity's sake
A straight line is determined by 2 distinct points (e.g. P0(x0, y0), P1(x1, y1)) - the equation for that straight line would be y = a * x + b (in our example: y = ((y0 - y1) / (x0 - x1)) * x + (y0 - x0 * ((y0 - y1) / (x0 - x1)))); !! Of course it worth mentioning the "vertical" (parallel to Oy) line which is !! not a function !!
Example: having 2 distinct parallel (non Bolyai :) ) lines: f0(x) = a * x + b0 and f1(x) = a * x + b1 (a is the same for both lines - this is the condition for them to be parallel) and an external point P0(x0, y0) (that obviously doesn't belong to any of the lines). How to determine if P0 is between the 2 lines? Well, the point must be above (the lower) one and below the other (the higher one). Translated into math (considering f0 being the lower one):
y0 > f0(x0) (y0 - f0(x0) > 0)
y0 < f1(x0) (y0 - f1(x0) < 0)
From the above observations (and there may be more wisdom), this is the condition that the point coordinates should satisfy: (y0 - f0(x0)) * (y0 - f1(x0)) < 0
Going further: a square consists of 2 sets of parallel lines (its sides); if a point is between each lines pair, then the point is in the square.
code00.py:
#!/usr/bin/env python3
import sys
from random import random, seed
from math import pi, sin, cos, sqrt
import matplotlib.pyplot as plt
pi_2 = pi / 2
MINX = MINY = 0
MAXX = MAXY = 1
DEFAULT_SIDE = 0.1
DEFAULT_SAFETY_MARGIN = DEFAULT_SIDE * sqrt(2)
MAX_SQUARES = 30
__global_generation_counter = 0
def get_func_deg1(p0, p1):
(x0, y0), (x1, y1) = p0, p1
if x0 == x1:
return None
a = (y0 - y1)/(x0 - x1)
b = y0 - x0 * a
return lambda x: a * x + b
def is_point_in_square(p, sq):
x, y = p
p0, p1, p2, p3 = sq
side_func0 = get_func_deg1(p0, p1)
side_func1 = get_func_deg1(p1, p2)
side_func2 = get_func_deg1(p2, p3)
side_func3 = get_func_deg1(p3, p0)
if not side_func0 or not side_func1 or not side_func2 or not side_func3:
xmin = min(p0[0], p2[0])
xmax = max(p0[0], p2[0])
ymin = min(p0[1], p2[1])
ymax = max(p0[1], p2[1])
return xmin <= x <= xmax and ymin <= y <= ymax
return ((y - side_func0(x)) * (y - side_func2(x))) <= 0 and \
((y - side_func1(x)) * (y - side_func3(x))) <= 0
def squares_overlap(square0, square1):
for p0 in square0:
if is_point_in_square(p0, square1):
return True
for p1 in square1:
if is_point_in_square(p1, square0):
return True
xc0 = (square0[0][0] + square0[2][0]) / 2
yc0 = (square0[0][1] + square0[2][1]) / 2
if is_point_in_square((xc0, yc0), square1):
return True
# The "reverse center check" not needed, since squares are congruent
"""
xc1 = (square1[0][0] + square1[2][0]) / 2
yc1 = (square1[0][1] + square1[2][1]) / 2
if is_point_in_square((xc1, yc1), square0):
return True
"""
return False
def __generation_monitor():
global __global_generation_counter
__global_generation_counter += 1
def generate_random_point(minx=MINX, miny=MINY, maxx=MAXX, maxy=MAXY, safety_margin=DEFAULT_SAFETY_MARGIN):
if maxx - minx < 2 * safety_margin or maxy - miny < 2 * safety_margin:
print("MUEEE")
safety_margin = 0
x = safety_margin + random() * (maxx - minx - 2 * safety_margin)
y = safety_margin + random() * (maxy - miny - 2 * safety_margin)
__generation_monitor()
return x, y
def generate_random_angle(max_val=pi_2):
return random() * max_val
def generate_random_square(side=DEFAULT_SIDE, squares_to_avoid=()):
while 1:
restart = False
x0, y0 = generate_random_point()
angle = generate_random_angle()
x1 = x0 + side * cos(angle)
y1 = y0 + side * sin(angle)
angle += pi_2
x2 = x1 + side * cos(angle)
y2 = y1 + side * sin(angle)
angle += pi_2
x3 = x2 + side * cos(angle)
y3 = y2 + side * sin(angle)
ret = (x0, y0), (x1, y1), (x2, y2), (x3, y3)
for square in squares_to_avoid:
if squares_overlap(ret, square):
restart = True
if restart:
continue
return ret
def square_to_plot(square):
xs, ys = zip(square[0], square[1], square[2], square[3])
return xs + (xs[0],), ys + (ys[0],)
def main():
seed()
squares = list()
allow_overlapping = False # CHANGE to True to allow square to overlap
for _ in range(MAX_SQUARES):
#print("Generating:", _)
if allow_overlapping:
square = generate_random_square()
else:
square = generate_random_square(squares_to_avoid=squares)
squares.append(square)
plot_squares = tuple()
for sq in squares:
plot_squares += square_to_plot(sq)
print("STATS:\n Squares: {}\n Allow overlapping: {}\n Generated values: {}".format(MAX_SQUARES, allow_overlapping, __global_generation_counter))
plt.plot(*plot_squares)
plt.axis([MINX, MAXX, MINY, MAXY])
plt.show()
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
Notes:
I didn't work with matplotlib before (actually, I pip installed it for this task)
General comments:
A point is represented by a tuple representing its coordinates: (x, y)
A square is a tuple consisting of 4 points (p0, p1, p2, p3)
get_func_deg1:
Returns the function that describes the line that contains the 2 points given as arguments
If the 2 points are on a line that is parallel to Oy (there's no "normal" function to describe it), simply return None
is_point_in_square:
Determines if a point is inside a square
Uses the logic explained above, except
For the special case when the square edges are parallel to Ox and Oy when it uses some simple arithmetic operations
squares_overlap:
Determines whether 2 squares overlap (I'm sure there are faster "algorithms")
Checks if any of the 1st square corners are inside the 2nd one
The other way around: checks if any of the 2nd square corners are inside the 1st one
Since the above 2 checks are not enough (imagine a regular [Wikipedia]: Octagon and unifying its vertices every 2nd one: there will be 2 squares neither having its corners in the other one, but sharing their "central" areas), also check that one square's center is inside the other one
generate_random_point:
Generates a point in the given bounding box
safety_margin specifies the (minimum) distance that the generated point should be away from any of the bounding box sides, in order that any square that will have this point as a corner, would fit entirely in the bounding box
generate_random_angle:
Generates a random angle between 0 and (π / 2)
generate_random_square:
Generates a random point, a random angle, and constructs a square starting from there, using the specified side
squares_to_avoid is a list of squares. After the square is generated, it is checked against every square from that list. If the 2 squares overlap, the square is regenerated
square_to_plot:
Converts a square (from a tuple of points) to matplotlib format (2 tuples consisting of xs and ys with the 1st element duplicated as the last)
main:
The main function
__generation_monitor (0):
Internal function used for profiling
In order to change the number of squares, modify MAX_SQUARES
Output:
[cfati#CFATI-5510-0:e:\Work\Dev\StackOverflow\q046081491]> "e:\Work\Dev\VEnvs\py_064_03.05.04_test0\Scripts\python.exe" code00.py
STATS:
Squares: 30
Allow overlapping: False
Generated values: 1135
Few words about the squares generation
As seen in the output, for 30 displayed squares, 1135 were generated (at this run). That is because they were overlapping
If changing from main allow_overlapping = True, Generated values in the output will match the number of squares (MAX_SQUARES)
If increasing MAX_SQUARES to values let's say higher than 50, the number of generated values will increase exponentially (so will the time needed to generate them), and the chance that the program will enter an infinite loop (because it won't be able to generate a square that doesn't overlap to another one) will grow as well
Okay, here is what I've come up with with a little help from the shapely package. Installation help is at the bottom here. The ultimate result:
Code walkthrough
distance is a helper function using the Point class from shapely to find the distance between two coordinates. Just a helper function for later.
Square instantiations a new polygon. It has 4 corners, each an (x,y) pair, one coordinate for its center, and a scalar value equal to half the distance of its diagonal.
test_overlap is pretty self explanatory by title. But logically what it does is this: find the distance from center-to-center between the two shapes. Then find the sum of the half-diagonal of each shape. If the center-to-center distance is greater than the sum, the squares cannot overlap.
Squares starts out with an empty container (empty list) and attempts to add squares to it. But for each possible new addition, it firsts tests that there is no overlap with existing squares.
Code
import math
import random
from shapely.geometry import Polygon, Point
def distance(a, b):
return Point(a).distance(Point(b))
class Square(object):
def __init__(self):
self.x0, self.y0 = random.random(), random.random()
theta = random.randint(0, 90) * math.pi / 180 # Angle of rotation
self.x1 = self.x0 + (0.1 * math.cos(theta))
self.x2 = self.x1 + (0.1 * math.cos((90 * math.pi/180) + theta))
self.x3 = self.x2 + (0.1 * math.cos((180 * math.pi/180) + theta))
self.y1 = self.y0 + (0.1 * math.sin(theta))
self.y2 = self.y1 + (0.1 * math.sin((90 * math.pi/180) + theta))
self.y3 = self.y2 + (0.1 * math.sin((180 * math.pi/180) + theta))
self.corners = ((self.x0, self.y0), (self.x1, self.y1),
(self.x2, self.y2), (self.x3, self.y3))
#property
def center(self):
"""(x, y) of the center of the polygon."""
return Polygon(self.corners).centroid.coords[0]
#property
def half_diag(self):
"""The distance of 1/2 the shape's diagonal (center-to-corner)."""
p0, p1, p2, p3 = self.corners
return 0.5 * distance(p0, p1) * math.sqrt(2)
def test_overlap(square1, square2):
"""Do two shapes overlap?
Note this is a 'conservative' test. May return True if they do not
(false positive), but will never return False if they do (false negative).
"""
# Distance between two centers
ctc = distance(square1.center, square2.center)
# Sum of half-diagonals
halfdiags = square1.half_diag + square2.half_diag
res = ctc < halfdiags
return res
class Squares(object):
def __init__(self):
self.squares = []
def add_square(self):
new_square = Square()
if not self.squares:
# Initial empty list/container - just add without any tests
self.squares.append(new_square)
else:
while True:
# Test that new_square overlaps with existing
res = [test_overlap(square, new_square) for square in self.squares]
if any(res):
# We have at least 1 case of overlap (1 True)
new_square = Square()
else:
# Safe to add
self.squares.append(new_square)
break
def plot_squares(self):
for square in self.squares:
(x0, y0), (x1, y1), (x2, y2), (x3, y3) = square.corners
plt.plot([x0, x1, x2, x3, x0], [y0, y1, y2, y3, y0])
Example
import itertools
%matplotlib inline
for _ in itertools.repeat(None, 10):
# Add 10 squares; you could also just build this into the class
sqs.add_square()
sqs.plot_squares()
Installing shapely
Install the Anaconda distribution if you don't already. Then just use conda-forge to install shapely. From cmd run:
conda config --add channels conda-forge
conda install shapely
Glaring deficiency
At a certain point, your container of squares gets filled up and there is minimal room left to add new shapes. Even if there is available space, the function is basically trial-and-error, so will take long at high shape counts. That happens at around 20-25 squares (in a 1.0x1.0 box) at the moment.
You need a function to determine whether two cube intersect.
from math import cos, pi, sin
from random import random
from matplotlib.mlab import frange
from matplotlib.pyplot import plot, axis, show,axes
LEN = 0.1
def rotate(point, theta):
x = point[0]
y = point[1]
x_ = x * cos(theta) + y * sin(theta)
y_ = - x * sin(theta) + y * cos(theta)
return x_, y_
class CUBE(object):
def __init__(self, x, y, theta):
self.corner = [(LEN / 2, LEN / 2),
(-LEN / 2, LEN / 2),
(-LEN / 2, -LEN / 2),
(LEN / 2, -LEN / 2)
]
self.theta = theta
self.x = x
self.y = y
for i in range(4):
self.corner[i] = rotate(self.corner[i], theta)
self.corner[i] = (self.corner[i][0] + x,
self.corner[i][1] + y)
def is_include(cube, point):
point = [point[0] - cube.x, point[1] - cube.y]
point = rotate(point, -cube.theta)
if (point[0] < -LEN / 2
or point[0] > LEN / 2
or point[1] < -LEN / 2
or point[1] > LEN / 2
):
return False
else:
return True
def is_intersect(cube1, cube2):
if (any([is_include(cube1, point) for point in cube2.corner])
or any([is_include(cube2, point) for point in cube1.corner])
or is_include(cube1, (cube2.x, cube2.y))):
return True
else:
return False
def plot_cube(cube,n):
plot(
[cube.corner[i][0] for i in [0, 1, 2, 3, 0]],
[cube.corner[i][1] for i in [0, 1, 2, 3, 0]])
ax = axes()
ax.text(cube.x,cube.y,str(n))
def display(cubelist): # connects the 4 corners on a plot
for i,cube in enumerate(cubelist):
plot_cube(cube,i)
axis([0, 1, 0, 1]) # 1x1 grid
show()
cubelist = []
for i in range(100):
x0 = random()
y0 = random()
theta = random() * pi
cube = CUBE(x0, y0, theta)
if any(is_intersect(cube,cb) for cb in cubelist):
continue
else:
cubelist.append(cube)
display(cubelist)

Circle and line collision detection in python tkinter

I writing a python program in which a circle bounces off of user drawn lines. There are multiple circles that bounce off the wall. For each one, the shortest distance from the center of the circle and the ball should be calculated. I would prefer if this code was very efficient because my current algorithm lags the computer a lot. If point a is the starting point ,and point b is the end point, and point c is the center, and r is the radius, how would I calculate the shortest distance between the ball? This algorithm should also work if the X coordinate of the ball is out of range of x coordinates in segment AB.
Please post python code
Any help would be appreciated!
Here's what I have so far:
lineList is a list with 4 values that contains beginning and end coordinates of the user drawn lines
center is the center of the ball
global lineList, numobjects
if not(0 in lineList):
beginCoord = [lineList[0],lineList[1]]
endCoord = [lineList[2]-500,lineList[3]-500]
center = [xCoordinate[i],yCoordinate[i]+15]
distance1 = math.sqrt((lineList[1] - center[1])**2 + (lineList[0] - center[0])**2)
slope1 = math.tan((lineList[1] - lineList[3]) / (lineList[0] - lineList[2]))
try:
slope2 = math.tan((center[1] - beginCoord[1])/(center[0]-beginCoord[0]))
angle1 = slope2 + slope1
circleDistance = distance1 * math.sin(angle1)
except:
#If the circle is directly above beginCoord
circleDistance = center[1] - lineList[1]
global numbounces
if circleDistance < 2 and circleDistance > -2:
print(circleDistance)
b = False
b2=False
if xCoordinate[i] < 0:
xCoordinate[i] += 1
speed1[i] *= -1
b=True
elif xCoordinate[i] > 0:
xCoordinate[i] -= 1
speed1[i] *= -1
b=True
if yCoordinate[i] < 0:
yCoordinate[i] += 1
speed2[i] *= -1
b2=True
elif yCoordinate[i] > 0:
yCoordinate[i] -= 1
speed2[i] *= -1
b2=True
if b and b2:
#Only delete the line if the ball reversed directions
numbounces += 1
#Add a ball after 5 bounces
if numbounces % 5 == 0 and numbounces != 0:
numobjects = 1
getData(numobjects)
canvas.delete("line")
lineList = [0,0,0,0]
I don't know what is the mean of shortest distance between the ball, but if you want to calculation the point where the circle will contact the line you can use sympy to figure the formula:
from sympy import *
from sympy.geometry import *
x1, y1, x2, y2, xc, yc = symbols("x1,y1,x2,y2,xc,yc")
p1 = Point(x1, y1)
p2 = Point(x2, y2)
pc = Point(xc, yc)
line = Line(p1, p2)
pline = line.perpendicular_line(pc)
p = line.intersection(pline)[0]
cse(p, symbols=numbered_symbols("t"))
the output is :
([(t0, x1 - x2), (t1, y1 - y2), (t2, x1*y2 - x2*y1), (t3, t0**2 + t1**2)],
[Point((t0**2*xc + t0*t1*yc - t1*t2)/t3, (t0*t1*xc + t0*t2 + t1**2*yc)/t3)])
this means that you can calculate the perpendicular point as:
t0 = x1 - x2
t1 = y1 - y2
t2 = x1*y2 - x2*y1
t3 = t0**2 + t1**2
xp = (t0**2*xc + t0*t1*yc - t1*t2)/t3
yp = (t0*t1*xc + t0*t2 + t1**2*yc)/t3

Categories

Resources