Issue w/ play() function in manim - python

I run this code:
from manimlib import *
class SquareToCircle(Scene):
def construct(self):
circle = Circle()
self.play(ShowCreation(circle))
self.play(circle.animate.shift(2 * RIGHT), circle.animate.scale(0.25))
It's shortened example from https://3b1b.github.io/manim/getting_started/quickstart.html. However, the circle doesn't shift right as specified with circle.animate.shift(2 * RIGHT) as seen here.

I found a correct way to do both animations at the same time
from manimlib import *
class SquareToCircle(Scene):
def construct(self):
circle = Circle()
self.play(ShowCreation(circle))
self.play(circle.animate.shift(2 * RIGHT).scale(0.25))
https://youtu.be/dwovjxTcvEk

Related

How can I use classes for python turtle to define a position? error message - TypeError: turtle.Vec2D() argument after * must be an iterable, not int

This is the code
import turtle
from turtle import *
class character:
The position is two values not one. You need to use a tuple or two parameters.
example:
import turtle
from turtle import *
class character:
def __init__(self,name, x, y=None ):
self.__name = name
self.__xposition = x
self.__yposition = y
self.__t = Turtle()
def help(self):
print(self.__name)
self.__t("turtle")
self.__t.goto(self.__xposition, self.__yposition)
player = character("hello", -150, 40)

AttributeError in python unit-test circle

I have to make unit-test for circle in python, but for the life of me cant figure out why I am getting this error :
Traceback (most recent call last):
File "c:\Users\User\Desktop\Domaci\Preadvanje 8\circle_unittest.py", line 6, in test_area
self.assertEqual(circle.area (2),12.564)
File "c:\Users\User\Desktop\Domaci\Preadvanje 8\class_circle.py", line 5, in area
return (self.radius * self.radius) * 3.141
AttributeError: 'int' object has no attribute 'radius'
Code for circle :
class circle:
def __init__(self,radius):
self.radius = radius
def area(self):
return (self.radius * self.radius) * 3.141
def perimeter(self):
return (2 * self.radius) * 3.141
r = int(input("Input r: "))
newcircle = circle(r)
print ("Area of the circle is: ", newcircle.area())
print("Perimeter of the circle is: ", newcircle.perimeter())
Code for test:
import unittest
from unittest.case import TestCase
from class_circle import circle
class test_circle(unittest.TestCase):
def test_area(self):
self.assertEqual(circle.area (2),12.564)
def test_perimeter(self):
self.assertEqual(circle.perimeter(2), 12.564)
if __name__ == '__main__':
unittest.main()
You are using the method "area" directly without instantiating a new object.
class test_circle(unittest.TestCase):
def test_area(self):
c = circle(2)
self.assertEqual(c.area (2),12.564)
def test_perimeter(self):
c = circle(2)
self.assertEqual(c.perimeter(2), 12.564)
In the above code, we create a circle called "c" first, and then test the method area on it, which fixes the issue.

3d rendering error whilst looking backwards

I made a 3d voxel rendering engine in python (turtle) a few days ago. Everything seems to be going smoothly, until you turn around 360∘ while in front of an object. The object will appear mirrored on the y axis. I came up with a fix for it via setting your y rotation, but that fix only works from one viewing angle. There are no console errors whatsoever, so I cannot pinpoint the location of the rendering error. I wish I had more info to aid in locating the issue.
from turtle import*
from time import*
from math import*
wn=Screen()
speed(0)
ht()
pu()
wn.tracer(0,0)
fov=200
xoff=0
yoff=0
zoff=0
camx=0
camy=0
camz=-105
xrot=0
yrot=0
zrot=0
jvel=0
onground=False
def goto3d(x,y,z):
rotxx=x
rotxy=y*cos(xrot)-z*sin(xrot)
rotxz=y*sin(xrot)+z*cos(xrot)
rotyx=rotxx*cos(yrot)+rotxz*sin(yrot)
rotyy=rotxy
rotyz=rotxz*cos(yrot)-rotxx*sin(yrot)
rotzx=rotyx*cos(zrot)-rotyy*sin(zrot)
rotzy=rotyx*sin(zrot)+rotyy*cos(zrot)
rotzz=rotyz
transx=rotzx-xoff
transy=rotzy-yoff
transz=rotzz-zoff
newx=fov*transx/transz
newy=fov*transy/transz
if newx<=-200 or newy<=-200 or newx>=200 or newy>=200:
pencolor('black')
else:
goto(newx,newy)
def cube(x,y,z):
z-=100
pu()
goto3d((-1+x)-camx,(-1+y)-camy,(-1+z)-camz)
pd()
goto3d((-1+x)-camx,(1+y)-camy,(-1+z)-camz)
goto3d((1+x)-camx,(1+y)-camy,(-1+z)-camz)
goto3d((1+x)-camx,(-1+y)-camy,(-1+z)-camz)
goto3d((-1+x)-camx,(-1+y)-camy,(-1+z)-camz)
pu()
goto3d((-1+x)-camx,(1+y)-camy,(-1+z)-camz)
pd()
goto3d((-1+x)-camx,(1+y)-camy,(1+z)-camz)
goto3d((1+x)-camx,(1+y)-camy,(1+z)-camz)
goto3d((1+x)-camx,(1+y)-camy,(1+z)-camz)
goto3d((1+x)-camx,(1+y)-camy,(-1+z)-camz)
pu()
goto3d((-1+x)-camx,(-1+y)-camy,(-1+z)-camz)
pd()
goto3d((-1+x)-camx,(-1+y)-camy,(1+z)-camz)
goto3d((1+x)-camx,(-1+y)-camy,(1+z)-camz)
goto3d((1+x)-camx,(-1+y)-camy,(1+z)-camz)
goto3d((1+x)-camx,(-1+y)-camy,(-1+z)-camz)
pu()
goto3d((-1+x)-camx,(1+y)-camy,(1+z)-camz)
pd()
goto3d((-1+x)-camx,(-1+y)-camy,(1+z)-camz)
pu()
goto3d((1+x)-camx,(1+y)-camy,(1+z)-camz)
pd()
goto3d((1+x)-camx,(-1+y)-camy,(1+z)-camz)
def black():
pencolor('black')
pu()
goto(-400,-400)
pd()
begin_fill()
goto(-200,200)
goto(200,200)
goto(200,-200)
end_fill()
def w():
global camz
global camx
camz+=.3*cos(yrot)
camx+=-1*.3*sin(yrot)
def a():
global camz
global camx
camz+=-1*.3*sin(yrot)
camx+=-1*.3*cos(yrot)
def s():
global camz
global camx
camz+=-1*.3*cos(yrot)
camx+=.3*sin(yrot)
def d():
global camz
global camx
camz+=.3*sin(yrot)
camx+=.3*cos(yrot)
def left():
global yrot
yrot+=pi/50
def right():
global yrot
yrot-=pi/50
def jump():
global jvel
if onground==True:
jvel=.3
wn.onkey(w,'w')
wn.onkey(a,'a')
wn.onkey(s,'s')
wn.onkey(d,'d')
wn.onkey(left,'Left')
wn.onkey(right,'Right')
wn.onkey(jump,'Space')
wn.listen()
def bush(x,y,z):
pencolor('darkgreen')
cube(0+x,0+y,0+z)
cube(2+x,0+y,0+z)
cube(2+x,2+y,0+z)
def tree(x,y,z):
pencolor('brown')
cube(0+x,0+y,0+z)
cube(0+x,2+y,0+z)
pencolor('green')
cube(0+x,4+y,0+z)
cube(0+x,6+y,0+z)
cube(2+x,4+y,0+z)
cube(-2+x,4+y,0+z)
cube(0+x,4+y,2+z)
cube(0+x,4+y,-2+z)
def render():
bush(-5,0,10)
tree(3,0,7)
while True:
clear()
black()
render()
update()
jvel-=.01
camy+=jvel
if camy<=0:
camy=0
jvel=.01
onground=True
if camy>=.3:
onground=False
You are not culling out vertices that lie behind the camera. I cannot see any evidence of culling in your code. If you render a point that lies behind the camera, it will appear upside down in the result. You are not actually turning 360 degrees, but 180 degrees. When the voxels are directly behind you, they will appear inverted if you don't cull it. You can test this by printing the yrot variable. Try modifying the line if newx<=-200 or newy<=-200 or newx>=200 or newy>=200 to include a check of transz, i.e. if transz < 0.1 or newx<=-200 or newy<=-200 or newx>=200 or newy>=200. Any small value will do but 0.1 should work well in your current implementation.

Circle supposed to move when you click on it but disappearing instead

For a classwork assignment everyone in the class had to make a program where when you click the shape it will disappear then reappear in a different location. However, it just disappears in mine. My teacher looked over it and saw no issues, but I still can't figure out how to fix it. Here is the code I wrote:
#-----import statements-----
import turtle as trtl
import random as rand
#-----game configuration----
dizzy_color = "red"
dizzy_size = 3
dizzy_shape = "circle"
#-----initialize turtle-----
dizzy = trtl.Turtle()
dizzy.shape(dizzy_shape)
dizzy.shapesize(dizzy_size)
dizzy.fillcolor(dizzy_color)
#-----game functions--------
def change_position():
new_xpos = rand.randint(-100, 100)
new_ypos = rand.randint(-50, 50)
dizzy.penup()
dizzy.hideturtle()
goto(new_xpos,new_ypos)
dizzy.showturtle()
dizzy.pendown()
def dizzy_clicked(x,y):
change_position()
#-----events----------------
dizzy.onclick(dizzy_clicked)
wn = trtl.Screen()
wn.mainloop()
Also the Turtle's name is dizzy.
The problem is you called the goto() method but forgot the object:
goto(new_xpos,new_ypos)
It should be:
dizzy.goto(new_xpos,new_ypos)
If you, or your teacher, are using a proper Python programming environment, you should have seen the related error message:
NameError: name 'goto' is not defined
Here's how I would go about writing this program:
from turtle import Screen, Turtle
from random import randint
DIZZY_COLOR = 'red'
DIZZY_SIZE = 3
DIZZY_SHAPE = 'circle'
def change_position():
new_xpos = randint(-100, 100)
new_ypos = randint(-50, 50)
dizzy.hideturtle()
dizzy.goto(new_xpos, new_ypos)
dizzy.showturtle()
def dizzy_clicked(x, y):
dizzy.onclick(None) # disable handler inside handler
change_position()
dizzy.onclick(dizzy_clicked)
screen = Screen()
dizzy = Turtle()
dizzy.penup()
dizzy.shape(DIZZY_SHAPE)
dizzy.shapesize(DIZZY_SIZE)
dizzy.fillcolor(DIZZY_COLOR)
dizzy.onclick(dizzy_clicked)
screen.mainloop()

Using VPython how to call Class sphere positions?

Through using VPython, I am able to get the program I am working on to generate multiple balls by calling the same class. I am also able to have the balls appear within a selected random range when they are generated (across x, y and z).
However, I am currently stumped on how I get to call the pos / position function from within my loop - as I would like to have the balls move.
Please see my code so far below.
If I call Ball.pos it states as undefined, but if I place my positioning via self.position, only one ball is generated, as they are not being referred to from under the sphere details?
from visual import *
from random import *
scene.title = "Multi Balls"
wallBm = box(pos=(0,-6,0), size=(12.2,0.1,12.1), color=color.blue, opacity=0.4)
vscale = 0.1
deltat = 0.005
t = 0
scene.autoscale = False
i = 0
totalBalls = 10
class Ball:
def __init__(self):
self.velocity = vector(0,5,0)
#vel sample ([5,10,15,20,25],3)
sphere (pos=(randrange (-6,6),randrange (-6,6),randrange (-6,6)), radius=0.5, color=color.cyan)
while True:
rate(100)
if i < totalBalls:
Ball()
i = i + 1
t = 5 + deltat
Try Inheritance from frame:
class Ball(frame):
def __init__(self, pos=(randrange (-6,6),randrange (-6,6),randrange (-6,6))):
frame.__init__(self,pos=pos)
self.velocity = vector(0,5,0)
sphere(frame=self,pos=pos,radius=0.5, color=color.cyan)
listOfBalls=[]
while True:
rate(100)
for i in range(totalBalls):
listOfBalls.append(Ball())
now try again!
You can call each Ball's position by calling listOfBalls[3].pos. I hope this helps!

Categories

Resources