I have written the following python code to create an animation of a rotating graph using python turtle. The problem I am facing is that, the animation is not properly synchronizing. I don't want to show the people exactly how the thing is rotating. So, I have to carefully choose the frame rate in the turtle.tracer command. Well, if you observe carefully, each value of r creates a rotation, so I must update the screen at the end of each r. And for each value of r, there are 1202 iterations inside the loop for r. But this does not produce the desired effect.
What must I do to correct this?
import turtle
import math
am = turtle
am.ht()
am.tracer(1202,0)
for r in range(0,600):
#axes
am.pu()
am.setpos(0,500)
am.pd()
am.setpos(0,-500)
am.pu()
am.setpos(-650,0)
am.pd()
am.setpos(0,0)
am.write("0",align="right",font=("Times New Roman",14,"normal"))
am.setpos(650,0)
am.pu()
am.setpos(-300*math.cos(r*math.pi/100),300*math.sin(r*math.pi/100))
am.pd()
#axes
am.pencolor("red")
for x in range(-300,301):
g=math.sin(x)
t=math.cos(x)
y =100*g*t*math.sin(2*(x**2)*math.pi/100)
am.setpos(x*math.cos(r*math.pi/100)+y*math.sin(r*math.pi/100),-x*math.sin(r*math.pi/100)+y*math.cos(r*math.pi/100))
#if(x%4==0):
#am.write(x)
am.pu()
am.setpos(-300*math.sin(r*math.pi/100),-300*math.cos(r*math.pi/100))
am.pd()
am.pencolor("blue")
for y in range(-300,301):
c=math.sin(y)
d=math.cos(y)
x =100*c*d*math.cos(2*(y**2)*math.pi/100)
am.setpos(x*math.cos(r*math.pi/100)+y*math.sin(r*math.pi/100),-x*math.sin(r*math.pi/100)+y*math.cos(r*math.pi/100))
am.reset()
am.exitonclick()
I believe the following will do what you desire. I've changed the tracer logic to be more manual with an explicit .update() when you're ready to show the user something. I've also separated the axis drawing from the main loop as we really don't need to clear and redraw it each time:
import math
from turtle import Turtle, Screen
screen = Screen()
screen.tracer(0)
# axes
axes = Turtle(visible=False)
axes.pu()
axes.setpos(0, 500)
axes.pd()
axes.setpos(0, -500)
axes.pu()
axes.setpos(-650, 0)
axes.pd()
axes.setpos(0, 0)
axes.write("0", align="right", font=("Times New Roman", 14, "normal"))
axes.setpos(650, 0)
am = Turtle(visible=False)
for r in range(0, 600):
am.pu()
am.setpos(-300 * math.cos(r * math.pi / 100), 300 * math.sin(r * math.pi / 100))
am.pd()
am.pencolor("red")
for x in range(-300, 301):
g = math.sin(x)
t = math.cos(x)
y = 100 * g * t * math.sin(2 * x**2 * math.pi / 100)
am.setpos(x * math.cos(r * math.pi / 100) + y * math.sin(r * math.pi / 100), -x * math.sin(r * math.pi / 100) + y * math.cos(r * math.pi / 100))
am.pu()
am.setpos(-300 * math.sin(r * math.pi / 100), -300 * math.cos(r * math.pi / 100))
am.pd()
am.pencolor("blue")
for y in range(-300, 301):
c = math.sin(y)
d = math.cos(y)
x = 100 * c * d * math.cos(2 * y**2 * math.pi / 100)
am.setpos(x * math.cos(r * math.pi / 100) + y * math.sin(r * math.pi / 100), -x * math.sin(r * math.pi / 100) + y * math.cos(r * math.pi / 100))
screen.update()
am.reset()
am.hideturtle() # made visible by reset()
screen.exitonclick()
Finally, this was likely an error:
am = turtle
and not doing what you think it was (module name alias instead of an actual turtle.)
Related
I need to generate a double 3D gyroid structure. For this, I'm using vedo
from matplotlib import pyplot as plt
from scipy.constants import speed_of_light
from vedo import *
import numpy as np
# Paramters
a = 5
length = 100
width = 100
height = 10
pi = np.pi
x, y, z = np.mgrid[:length, :width, :height]
def gen_strut(start, stop):
'''Generate the strut parameter t for the gyroid surface. Create a linear gradient'''
strut_param = np.ones((length, 1))
strut_param = strut_param * np.linspace(start, stop, width)
t = np.repeat(strut_param[:, :, np.newaxis], height, axis=2)
return t
plt = Plotter(shape=(1, 1), interactive=False, axes=3)
scale=0.5
cox = cos(scale * pi * x / a)
siy = sin(scale * pi * y / a)
coy = cos(scale * pi * y / a)
siz = sin(scale * pi * z / a)
coz = cos(scale * pi * z / a)
six = sin(scale * pi * x / a)
U1 = ((six ** 2) * (coy ** 2) +
(siy ** 2) * (coz ** 2) +
(siz ** 2) * (cox ** 2) +
(2 * six * coy * siy * coz) +
(2 * six * coy * siz * cox) +
(2 * cox * siy * siz * coz)) - (gen_strut(0, 1.3) ** 2)
threshold = 0
iso1 = Volume(U1).isosurface(threshold).c('silver').alpha(1)
cube = TessellatedBox(n=(int(length-1), int(width-1), int(height-1)), spacing=(1, 1, 1))
iso_cut = cube.cutWithMesh(iso1).c('silver').alpha(1)
# Combine the two meshes into a single mesh
plt.at(0).show([cube, iso1], "Double Gyroid 1", resetcam=False)
plt.interactive().close()
The result looks quite good, but now I'm struggling with exporting the volume. Although vedo has over 300 examples, I did not find anything in the documentation to export this as a watertight volume for 3D-Printing. How can I achieve this?
I assume you mean that you want to extract a watertight mesh as an STL (?).
This is a non trivial problem because it is only well defined on a subset of the mesh regions where the in/out is not ambiguous, in those cases fill_holes() seems to do a decent job..
Other cases should be dealt "manually". Eg, you can access the boundaries with mesh.boundaries() and try to snap the vertices to a closest common vertex. This script is not a solution, but I hope can give some ideas on how to proceed.
from vedo import *
# Paramters
a = 5
length = 100
width = 100
height = 10
def gen_strut(start, stop):
strut_param = np.ones((length, 1))
strut_param = strut_param * np.linspace(start, stop, width)
t = np.repeat(strut_param[:, :, np.newaxis], height, axis=2)
return t
scale=0.5
pi = np.pi
x, y, z = np.mgrid[:length, :width, :height]
cox = cos(scale * pi * x / a)
siy = sin(scale * pi * y / a)
coy = cos(scale * pi * y / a)
siz = sin(scale * pi * z / a)
coz = cos(scale * pi * z / a)
six = sin(scale * pi * x / a)
U1 = ((six ** 2) * (coy ** 2) +
(siy ** 2) * (coz ** 2) +
(siz ** 2) * (cox ** 2) +
(2 * six * coy * siy * coz) +
(2 * six * coy * siz * cox) +
(2 * cox * siy * siz * coz)) - (gen_strut(0, 1.3) ** 2)
iso = Volume(U1).isosurface(0).c('silver').backcolor("p5").lw(1).flat()
cube = TessellatedBox(n=(length-1, width-1, height-1)).c('red5').alpha(1)
cube.triangulate().compute_normals()
cube.cut_with_mesh(iso).compute_normals()
print(iso.boundaries(return_point_ids=True))
print(cube.boundaries(return_point_ids=True))
print(iso.boundaries().join().lines())
show(iso, cube).close()
merge(iso, cube).clean().backcolor("p5").show().close()
iso.clone().fill_holes(15).backcolor("p5").show().close()
As there are many questions (this, this and this) about axis grid in mayavi that how to get matplotlib type grid using mayavi and there is no satisfactory answer even from Mayavi updates.
So regarding above problem, there is an idea that we can add ground plane in Mayavi as object and then we can draw/show required objects on ground plane. Below is object that I draw as ground plane
Code for above draw ground is
x, y = np.mgrid[-10:10:200j, -10:10:200j]
z = np.sin(x * y) / (x * y)
mlab.figure(bgcolor=(1, 1, 1))
mlab.surf(z, colormap='cool')
mlab.show()
Let's say we have an object that we want to draw/show it on above ground plane.
Below is object (as a example)
Code for above object is
mlab.figure(fgcolor=(0, 0, 0), bgcolor=(1, 1, 1))
u, v = mgrid[- 0.035:pi:0.01, - 0.035:pi:0.01]
X = 2 / 3. * (cos(u) * cos(2 * v) + sqrt(2) * sin(u) * cos(v)) * cos(u) / (sqrt(2) - sin(2 * u) * sin(3 * v))
Y = 2 / 3. * (cos(u) * sin(2 * v) - sqrt(2) * sin(u) * sin(v)) * cos(u) / (sqrt(2) - sin(2 * u) * sin(3 * v))
Z = -sqrt(2) * cos(u) * cos(u) / (sqrt(2) - sin(2 * u) * sin(3 * v))
S = sin(u)
mlab.mesh(X, Y, Z, scalars=S, colormap='YlGnBu', )
mlab.view(.0, - 5.0, 4)
mlab.show()
This code is available here
So how we can draw given object on the given ground plane same like below image? I edit this image in photoshop for better view.
Looking for some kind suggestions.
Ive create a bilateral filter in python and added numba to(in theory) run it on the my GPU. Yet when I set the mode to CUDA its extremely slow and doesn't seem to use my graphics card at all while putting it into parallel makes it very fast. The best reason I have is that my function just isn't optimized to run on my GPU very well because it can't apply one function to all elements at once(because it requires the x and y coordinate to work). So my question is, why does this run slow with target set to CUDA?
#guvectorize([(float64[:,:], float64[:,:])],'(n,m)->(n,m)',target='cuda',nopython =True)
def apply_filter(img, filteredImage):
imh, imw = img.shape[:2]
radius = diameter // 2
hd = int((diameter - 1) / 2)
#print("starting work in cuda")
for h in prange(hd, imh - hd):
for w in prange(hd, imw - hd):
Wp = 0
filteredPixel = 0
startX = h
startY = w
for x in prange(0, diameter):
for y in prange(0, diameter):
currentX = startX - (radius - x)
cureentY = startY - (radius - y)
intensityDifferent = img[currentX][cureentY] - img[startX][startY]
intensity = (1.0 / (2 * math.pi * (sIntesity ** 2)) * math.exp(- (intensityDifferent ** 2) / (2 * sIntesity ** 2)))
distance = ((currentX-startX)**2 + (cureentY-startY)**2)**(1/2)
smoothing = (1.0 / (2 * math.pi * (sSpace ** 2))) * math.exp(- (distance ** 2) / (2 * sSpace ** 2))
weight = intensity * smoothing;
filteredPixel += img[currentX][cureentY] * weight
Wp += weight
filteredImage[h][w] = int(round(filteredPixel / Wp))
#print("done!")
I want to "animate" a circle rolling over the sin graph, I made a code where the circle moves rapidly down a straight line, now I want the same but the acceleration will be changing.
My previous code:
import numpy as np
import matplotlib.pyplot as plt
theta = np.arange(0, np.pi * 2, (0.01 * np.pi))
x = np.arange(-50, 1, 1)
y = x - 7
plt.figure()
for t in np.arange(0, 4, 0.1):
plt.plot(x, y)
xc = ((-9.81 * t**2 * np.sin(np.pi / 2)) / 3) + (5 * np.cos(theta))
yc = ((-9.81 * t**2 * np.sin(np.pi / 2)) / 3) + (5 * np.sin(theta))
plt.plot(xc, yc, 'r')
xp = ((-9.81 * t**2 * np.sin(np.pi / 2)) / 3) + (5 * np.cos(np.pi * t))
yp = ((-9.81 * t**2 * np.sin(np.pi / 2)) / 3) + (5 * np.sin(np.pi * t))
plt.plot(xp, yp, 'bo')
plt.pause(0.01)
plt.cla()
plt.show()
You can do this by numerically integrating:
dt = 0.01
lst_x = []
lst_y = []
t = 0
while t < 10: #for instance
t += dt
a = get_acceleration(function, x)
x += v * dt + 0.5 * a * dt * dt
v += a * dt
y = get_position(fuction, x)
lst_x.append(x)
lst_y.append(y)
This is assuming the ball never leaves your slope! If it does, you'll also have to integrate in y in a similar way as done in x!!
Where your acceleration is going to be equal to g * cos(slope).
I have an image read in with python and I wish to add some different sine stripes on this image as a noise. I wish the frequency and the rotation degree of a sine is totally random. I tried numpy module but it might not be the model I need here.
Can anyone tell me any python module has such function to add random sine curve to a image?
The result should somewhat similar to this image below:
Finally I find I can finish all of this using Numpy module:
def sineimg(img, color='black', linewidth=1.5, linestyle="-"):
'''
frequency = X / random.uniform(10.0, 20.0)
amplitude = randint(35, 50)
phase = random.uniform(1.0, 16.0)
rotation = random.uniform(-pi / 2, pi / 2)
'''
#Random rotation angle
rot = random.randint(-90, 90)
x_ = img.shape[0] / np.cos(np.pi * rot / 180)
X = np.linspace(-1 * x_, x_, 512)
axes = plt.subplot(111)
np.cos(np.pi * rot / 180)
#Random amplitude
amp1 = random.randint(35, 50)
amp2 = random.randint(35, 50)
#Random frequency
frequency1 = X / random.uniform(10.0, 20.0)
frequency2 = X / random.uniform(10.0, 20.0)
#Random offset phase
phase1 = np.pi / random.uniform(1.0, 16.0) + np.pi / 2
phase2 = np.pi / random.uniform(1.0, 16.0) + np.pi / 3
#random distance between line cluster
distance = random.randint(90, 115)
#I need roughly 8 times of them
for i in range(8):
Y1 = amp1 * np.sin(frequency1 + phase1) - 450 + i * distance
Y2 = amp2 * np.sin(frequency2 + phase2) - 420 + i * distance
x1_trans = X * np.cos(np.pi * rot / 180) - Y1* np.sin(np.pi * rot / 180)
y1_trans = X * np.sin(np.pi * rot / 180) + Y1* np.cos(np.pi * rot / 180)
x2_trans = X * np.cos(np.pi * rot / 180) - Y2* np.sin(np.pi * rot / 180)
y2_trans = X * np.sin(np.pi * rot / 180) + Y2* np.cos(np.pi * rot / 180)
#Remove label
axes.set_xticks([])
axes.set_yticks([])
axes.spines['right'].set_color('none')
axes.spines['top'].set_color('none')
axes.spines['bottom'].set_color('none')
axes.spines['left'].set_color('none')
axes.plot(x1_trans, y1_trans, color = color, linewidth=linewidth, linestyle=linestyle)
axes.plot(x2_trans, y2_trans, color = color, linewidth=linewidth, linestyle=linestyle)
plt.imshow(img, zorder=0, extent=[148, -148, -225, 225])
Them:
img=mpimg.imread('me.jpg')
sineimg(img)
I get images like: