python move circle around another circle in graphics.py - python

I am writing a program in Python using graphics.py library. I want to draw two circles, and then in loop move one of them around another one. I know I have to use sin and cos function, but I have no idea what is a mathematical formula for that.
That's my code:
from graphics import *
from math import sin, cos, pi
from time import sleep
win = GraphWin('Program', 500, 500)
win.setBackground('white')
c = Circle(Point(250, 250), 50)
c.draw(win)
c1 = Circle(Point(250, 175), 25)
c1.draw(win)
while True:
c1.move() #there I have to use some formula for moving circle c1 around circle c
sleep(1)
win.getMouse()
win.close()

A bit of mathematics have to be used. Based on your example, you want the circles to be adjacent to each other.
Because of that, distance between their centres will always be r1+r2. This is a length of our vector. We need to split that vector into x axis and y axis parts. This is where sine and cosine functions come in, value it of it at a given angle will mean how far along that axis we have to move our center.
After calculating new position all we have to do is subtract that from current position to see how far we need to move our other circle.
from graphics import *
from math import sin, cos, pi
from time import sleep
win = GraphWin('Program', 500, 500)
win.setBackground('white')
c_origin_x = 250
c_origin_y = 250
c_radius = 50
c = Circle(Point(c_origin_x, c_origin_y), c_radius)
c.draw(win)
c1_oldpos_x = c_origin_x # doesn't actually matter, it gets moved immediately
c1_oldpos_y = c_origin_y
c1_radius = 25
c1 = Circle(Point(c1_oldpos_x, c1_oldpos_y), c1_radius)
c1.draw(win)
angle = 0 # initial angle
while True:
c1_newpos_x = sin(angle) * (c_radius + c1_radius) + c_origin_x
c1_newpos_y = cos(angle) * (c_radius + c1_radius) + c_origin_y
c1.move(c1_newpos_x - c1_oldpos_x, c1_newpos_y - c1_oldpos_y)
sleep(1)
angle += 0.1 * pi # this is what will make a position different in each iteration
c1_oldpos_x = c1_newpos_x
c1_oldpos_y = c1_newpos_y
win.getMouse()
win.close()

Related

How to get the points of a bezier curve/parabola using 3 unique points on a graph

I'm trying to make a parabolic/bezier curve between two main points (using a third point as the control point) and cannot work out how to do it.
from turtle import *
pointA = (0.00,50.00)
pointB = (0.00,350.00)
pointC = (-300.00,50.00)
pu()
goto(pointB)
pd()
dot()
goto(pointC)
dot()
ht()
this creates a line between the 2 points that are my main points,
i also want to make a curve using pointA so i can have multiple lines,
i have already ruled out the possibility of parabolic curves because that equation doesn't fit on a parabola unless i rotate the plane but thats a whole other kettle of fish i'm not ready for.
i would love some help as i am decently stuck,
thank you
Edit:
i have tried a fair few things none of them got close, i ended up resorting to just running with a midpoint translated a few pixels. E.G.
for j in range(3):
pu()
goto(pointB)
pd()
dot()
midpoint = ((pointB[0]+pointC[0])/2, (pointB[1]+pointC[1])/2)
goto(midpoint[0]+(20*j), midpoint[1])
goto(pointC)
dot()
this is a more realistic use of what im using it for, except i want to change that solid line into a variable line as dependednt on the position of the two points it will be on the same line, thus making it look like 1 singular line.
Based on Wikipedia's explanation of quadratic Bézier curves we should be able to simply do:
from turtle import Screen, Turtle, Vec2D
p0 = Vec2D(0, 50)
p1 = Vec2D(-300, 50)
p2 = Vec2D(0, 350)
b = lambda t: p1 + (1 - t)**2 * (p0 - p1) + t**2 * (p2 - p1)
turtle = Turtle()
turtle.penup()
for position in [p2, p1, p0]:
turtle.goto(position)
turtle.dot()
turtle.pendown()
t = 0
while t <= 1:
position = b(t)
turtle.setheading(turtle.towards(position))
turtle.goto(position)
t += 0.1
screen = Screen()
screen.exitonclick()

Pyautogui: Mouse Movement with bezier curve

I'm trying to move the mouse in a bezier curve motion in Pyautogui to simulate more of a human movement as seen here:
There is some tweening / easing functions within pyautogui but none of which represent a bezier curve type movement. I created a small script to calculate the random places it will hit before ultimately hitting its destination.
Default "Robot" linear path:
Unfortunately, which each destination the mouse temporarily stops.
import pyautogui
import time
import random
print "Randomized Mouse Started."
destx = 444;
desty = 631;
x, y = pyautogui.position() # Current Position
moves = random.randint(2,4)
pixelsx = destx-x
pixelsy = desty-y
if moves >= 4:
moves = random.randint(2,4)
avgpixelsx = pixelsx/moves
avgpixelsy = pixelsy/moves
print "Pixels to be moved X: ", pixelsx," Y: ",pixelsy, "Number of mouse movements: ", moves, "Avg Move X: ", avgpixelsx, " Y: ", avgpixelsy
while moves > 0:
offsetx = (avgpixelsx+random.randint(-8, random.randint(5,10)));
offsety = (avgpixelsy+random.randint(-8, random.randint(5,10)));
print x + offsetx, y + offsety, moves
pyautogui.moveTo(x + offsetx, y + offsety, duration=0.2)
moves = moves-1
avgpixelsx = pixelsx / moves
avgpixelsy = pixelsy / moves
Info:
Windows 10
Python 2.7
Willing to use other libraries, Python version if necessary
I've seen this post: python random mouse movements
but can't figure out how to define a "start and stop" position. The answer is pretty close to what I'm looking for.
Any ideas on how to accomplish this?
Using scipy, numpy and anything that can simply move mouse cursor:
import pyautogui
import random
import numpy as np
import time
from scipy import interpolate
import math
def point_dist(x1,y1,x2,y2):
return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
cp = random.randint(3, 5) # Number of control points. Must be at least 2.
x1, y1 = pyautogui.position() # Starting position
# Distribute control points between start and destination evenly.
x = np.linspace(x1, x2, num=cp, dtype='int')
y = np.linspace(y1, y2, num=cp, dtype='int')
# Randomise inner points a bit (+-RND at most).
RND = 10
xr = [random.randint(-RND, RND) for k in range(cp)]
yr = [random.randint(-RND, RND) for k in range(cp)]
xr[0] = yr[0] = xr[-1] = yr[-1] = 0
x += xr
y += yr
# Approximate using Bezier spline.
degree = 3 if cp > 3 else cp - 1 # Degree of b-spline. 3 is recommended.
# Must be less than number of control points.
tck, u = interpolate.splprep([x, y], k=degree)
# Move upto a certain number of points
u = np.linspace(0, 1, num=2+int(point_dist(x1,y1,x2,y2)/50.0))
points = interpolate.splev(u, tck)
# Move mouse.
duration = 0.1
timeout = duration / len(points[0])
point_list=zip(*(i.astype(int) for i in points))
for point in point_list:
pyautogui.moveTo(*point)
time.sleep(timeout)
And you can remove any built-in delay in pyautogui by setting:
# Any duration less than this is rounded to 0.0 to instantly move the mouse.
pyautogui.MINIMUM_DURATION = 0 # Default: 0.1
# Minimal number of seconds to sleep between mouse moves.
pyautogui.MINIMUM_SLEEP = 0 # Default: 0.05
# The number of seconds to pause after EVERY public function call.
pyautogui.PAUSE = 0 # Default: 0.1
P.S.: Example above doesn't require any of those settings as it doesnt use public moveTo method.
For a simple solution, you can try using numpy with the bezier library:
import pyautogui
import bezier
import numpy as np
# Disable pyautogui pauses (from DJV's answer)
pyautogui.MINIMUM_DURATION = 0
pyautogui.MINIMUM_SLEEP = 0
pyautogui.PAUSE = 0
# We'll wait 5 seconds to prepare the starting position
start_delay = 5
print("Drawing curve from mouse in {} seconds.".format(start_delay))
pyautogui.sleep(start_delay)
# For this example we'll use four control points, including start and end coordinates
start = pyautogui.position()
end = start[0]+600, start[1]+200
# Two intermediate control points that may be adjusted to modify the curve.
control1 = start[0]+125, start[1]+100
control2 = start[0]+375, start[1]+50
# Format points to use with bezier
control_points = np.array([start, control1, control2, end])
points = np.array([control_points[:,0], control_points[:,1]]) # Split x and y coordinates
# You can set the degree of the curve here, should be less than # of control points
degree = 3
# Create the bezier curve
curve = bezier.Curve(points, degree)
# You can also create it with using Curve.from_nodes(), which sets degree to len(control_points)-1
# curve = bezier.Curve.from_nodes(points)
curve_steps = 50 # How many points the curve should be split into. Each is a separate pyautogui.moveTo() execution
delay = 1/curve_steps # Time between movements. 1/curve_steps = 1 second for entire curve
# Move the mouse
for i in range(1, curve_steps+1):
# The evaluate method takes a float from [0.0, 1.0] and returns the coordinates at that point in the curve
# Another way of thinking about it is that i/steps gets the coordinates at (100*i/steps) percent into the curve
x, y = curve.evaluate(i/curve_steps)
pyautogui.moveTo(x, y) # Move to point in curve
pyautogui.sleep(delay) # Wait delay
I came up with this trying to write something to draw SVG Paths with the mouse. Running the above code will make your mouse move along the same path as below. The red dots are positioned at each of the control points that define the curve.
Note that you'll have to add pyautogui.mouseDown() before and pyautogui.mouseUp() after the loop at the end of the script if you want to click and drag like I did here in GIMP:
You can check out the bezier docs here: https://bezier.readthedocs.io/en/stable/index.html
you just need know is the move_mouse((300,300))will let you mouse arrive (300,300),then never change.look at the implement,it just call the WIN32 api mouse_event.read something about it,you will find there are no "start and stop" position.i don't know how to draw bezier curve.
while True:
pos = (random.randrange(*x_bound),random.randrange(*y_bound))
move_mouse(pos)
time.sleep(1.0/steps_per_second)
look,that is the secret of animation.all you need do is write a pos = draw_bezier_curve(t)

Using code to write a function that calculates values for Projectile motion

I have an assignment that asks me to use some given code to write a function which calculates the angle needed to hit a target 10 metres away.
here is the given code:
from visual import *
from visual.graph import * # For the graphing functions
#Create a graph display window (gdisplay)
win = gdisplay(xtitle="Distance [m]", ytitle="Height [m]")
#And a curve on this display
poscurve = gcurve(gdisplay=win, color=color.cyan)
#Target position (10 meters away)
target_pos = vector(10,0,0)
#Set the starting angle (in degrees)
angle = 45
#Set the magnitude of the starting velocity (in m/s)
v0 = 12.0
#Gravity vector (m/s**2)
g = vector(0, -9.8, 0)
#Create a vector for the projectile's velocity
velocity = v0 * vector(cos(anglepi/180), sin(anglepi/180), 0)
#and the position
position = vector(0,0,0)
dt = 0.01 # Time step
#Start loop. Each time taking a small step in time
while (position.y > 0) or (velocity.y > 0): # Change in position # dx = (dx/dt) * dt dx = velocity * dt
# Change in velocity
# dv = (dv/dt) * dt
dv = g * dt
# Update the position and velocity
position = position + dx
velocity = velocity + dv
# Plot the current position
poscurve.plot(pos=position)
#When loop finishes, velocity.y must be < 0, and position.y < 0
print "Landed at X position: ", position.x print "X distance to target: ", position.x - target_pos.x
How would I now write a function to calculate the required value? I have no idea where to start, any help would be greatly appreciated!
Thanks
You could use maths to work out an equation for the result.
This works out as:
range = 2v^2/g *cos(a)sin(a)
where v=initial velocity
a=angle
g=gravitational acceleration
You can use this python script to find the answer:
from math import cos
from math import sin
from math import radians
from math import fabs
a=0 # angle in degrees
target=10 # How far you want it to go in m
v=12 # initial velocity in m/s
g=9.81 #gravitational acceleration m/s/s
best_angle=None
nearest_answer=None
while a<45: # we only need to check up to 45 degrees
r = 2*v*v/g*cos(radians(a))*sin(radians(a))
if not nearest_answer or fabs(r-target)<fabs(nearest_answer-target):
nearest_answer = r
best_angle = a
print("{0} -> {1}".format(a,r))
a+=.1 # try increasing the angle a bit. The lower this is the more accurate the answer will be
print("Best angle={}".format(best_angle))

homework help? for making a spirograph

SO over break week our teacher gave us a little project to do requiring a Spirograph, here is the code he helped us write before
from graphics import *
from math import *
def ar(a):
return a*3.141592654/180
def main():
x0 = 100
y0 = 100
startangle = 60
stepangle = 120
radius = 50
win = GraphWin()
p1 = Point(x0 + radius * cos(ar(startangle)), y0 + radius * sin(ar(startangle)))
for i in range(stepangle+startangle,360+stepangle+startangle,stepangle):
p2 = Point(x0 + radius * cos(ar(i)), y0 + radius * sin(ar(i)))
Line(p1,p2).draw(win)
p1 = p2
input("<ENTER> to quit...")
win.close()
main()
he then wants us to develop the program that consecutively draws 12 equilateral triangles (rotating the triangle each time by 30 degrees through a full 360 circle). This can be achieved by “stepping” the STARTANGLE parameter. My question I am stuck on where to go from here, what does he mean by "stepping?" I assume making some sort of loop, is it possible someone can give me a push in the right step?
This is a solution using matplotlib. The general procedure would be the same. But you will have to modify it for using the libraries you're allowed to use.
from math import radians, sin, cos
import matplotlib.pyplot as plt
startAngle = 0
stepAngle = 30
origin = (0,0)
points = []
points.append(origin)
points.append((cos(radians(startAngle)), sin(radians(startAngle))))
for i in range(startAngle + stepAngle, 360 + stepAngle, stepAngle):
x = cos(radians(i))
y = sin(radians(i))
points.append((x,y))
points.append(origin)
points.append((x,y))
x,y = zip(*points) #separate the tupples into x and y coordinates.
plt.plot(x,y) #plots the points, drawing lines between each point
plt.show()
plt.plot draw lines between each point in the list. We're adding inn the origin points so we get triangles instead of just a polygon around the center.

Randomly orientated lines drawn off a random point in python

I'm trying to create python program that has several vertical lines which act as boundaries where randomly generated points or "dots" (as referred to in the code) which draw a straight line at a random degree. If the straight line intersects with one of the vertical "boundaries" I want to make it change colour. I have a picture of what I am trying to achieve which will probably explain my situation a bit clearer. The code I post below has drawn the "vertical boundaries" and has the points randomly generated within the region, however that is where I am stuck.
What I am aiming to achieve:
Example of program
My current Code:
setup(750,750)
screen_size = 750
max_coord = (screen_size - 30) / 2
### change the number of dots you have via that variable
num_dots = 500
bgcolor('yellow')
dot_size=5
reset() # Create an empty window
pi = Turtle()
hideturtle()
def parallel_lines(number):
pi.pensize(2)
pi.pencolor('black')
width = pi.window_width()
height = pi.window_height()
pi.setheading(90)
pi.penup()
pi.setposition(width/-2, height/-2)
for i in range(1, number +2):
pi.pendown()
pi.forward(height)
pi.penup()
pi.setposition(width/-2+i*(width/(number+1)),height/-2)
parallel_lines(7)
## centre turtle back in the middle of the page
goto(0,0)
### list to hold the dots
x_coords = []
y_coords = []
### Draw the dots via randomint
penup()
color("blue")
for dot_num in range(num_dots):
dot_pos_x = randint (-max_coord, max_coord)
dot_pos_y = randint (-max_coord, max_coord)
goto(dot_pos_x, dot_pos_y)
dot(dot_size)
x_coords.append(dot_pos_x)
y_coords.append(dot_pos_y)
done()
Thank you in advance for anyone that can help.
Here's an implementation of the program the OP describes. If there is a line intersection, it uses Python 3 turtle's undo feature to remove the line and redraw it in the alternate color:
from turtle import Turtle, Screen
from random import randint, randrange
SCREEN_SIZE = 750
PLANK_COUNT = 8
PINHEAD_SIZE = 5
FLOOR_COLOR = "yellow"
DEFAULT_COLOR = "blue"
CROSSING_COLOR = "red"
screen = Screen()
screen.setup(SCREEN_SIZE, SCREEN_SIZE)
screen.bgcolor(FLOOR_COLOR)
# configure numbers to replicate Lazzarini's setup
NUMBER_PINS = 3408
PIN_LENGTH = 78.125
PLANK_WIDTH = screen.window_width() / PLANK_COUNT
def parallel_lines(turtle, width, height):
turtle.penup()
turtle.setheading(90)
turtle.sety(height / -2)
x_coordinates = []
for i in range(PLANK_COUNT + 1):
x = i * PLANK_WIDTH - width / 2
turtle.setx(x)
turtle.pendown()
turtle.forward(height)
turtle.penup()
turtle.left(180)
x_coordinates.append(x)
return x_coordinates
pi = Turtle(visible=False)
pi.speed("fastest")
x_coordinates = parallel_lines(pi, screen.window_width(), screen.window_height())
def crosses(x0, x1, coordinates):
for coordinate in coordinates:
if x0 <= coordinate <= x1 or x1 <= coordinate <= x0:
return True
return False
previous_crossings = crossings = 0
max_coord = screen.window_width() / 2
for pin in range(NUMBER_PINS):
x0, y0 = randint(-max_coord, max_coord), randint(-max_coord, max_coord)
pi.color(DEFAULT_COLOR)
pi.goto(x0, y0)
pi.dot(PINHEAD_SIZE)
pi.setheading(randrange(360))
pi.pendown()
pi.forward(PIN_LENGTH)
if crosses(x0, pi.xcor(), x_coordinates):
pi.undo()
pi.color(CROSSING_COLOR)
pi.dot(PINHEAD_SIZE)
pi.forward(PIN_LENGTH)
crossings += 1
pi.penup()
if previous_crossings != crossings:
estimate = (2 * PIN_LENGTH * pin) / (PLANK_WIDTH * crossings)
print(estimate)
previous_crossings = crossings
screen.exitonclick()
Now for the rest of the story. What the OP didn't mention is that this is a drawing of planks in a floor, and we're dropping pins onto it, keeping track of how many cross lines in the flooring, as a means of estimating the value of PI (π)!
Read about Buffon's needle for details. The gist is the probability of a pin crossing a line is a function of PI so we can turn the equation around, drop actual (or virtual) pins, to estimate PI.
The program outputs the running estimate for PI (π), based on pins dropped so far, to the console:
...
3.121212121212121
3.1215970961887476
3.1370772946859904
3.134418324291742
3.131768953068592
3.1381381381381384
3.1384892086330933
3.1358467983243568
3.1451612903225805
3.1454979129397733
3.1458333333333335
3.1491384432560903
3.1465005931198102
3.1438721136767316
3.144208037825059
3.144542772861357
3.1419316843345113

Categories