Why is application of complex transformations in manim behaving wierdly? - python

The complex transformation of the line should map the line to a circle passing through the origin, also the complex transformation of the circle centered at 1,0,0 of radius 1 should map to a line but it is behaving weirdly.
from manim import *
#config['frame_height'] = 10.0
#config['frame_width'] = 10.0
class Method(Scene):
def construct(self):
text = Tex(r"Applying Complex Transformations")
self.play(Create(text))
class Complex(Scene):
def construct(self):
d=ComplexPlane()
k=d.copy()
self.play(Create(d))
self.add(k)
line = Line(start=[2,0,0],end=[2,5,0],stroke_width=3,color=RED)
circle = Circle().shift(RIGHT)
self.add(line)
c = Circle().shift(RIGHT)
self.play(c.animate.apply_complex_function(lambda z:z**2)) #works correctly
self.play(line.animate.apply_complex_function(lambda z:1/z),run_time=5) #behaving wierdly
self.play(circle.animate.apply_complex_function(lambda z:1/z),run_time=5) #behaving wierdly
applied to line
applied to circle

Well the function 1/z diverges at z=0, therefore, every point that starts at that position (or near it) will have problems and give you weird artifacts. I tried your code and the line seems fine to me, but the circle is the one that might do weird stuff, and it just so happens that the circle goes through (0,0) in the complex plane. Try Shifting it some more so that it doesn't happen.
circle = Circle().shift(RIGHT*1.5)
you can also of course, change the function so that it doesn't diverge at z=0. Something like 1/(z+2) should work

Related

Find an object that other canvas object is overlapping with - Python tkinter

I want to detect with what canvas object block. We have block, circle and triangle canvas objects.
I know there is if block in canvas.find_overlapping(x1,y1,x2,y2): method but doesn't shows with what object is block overlapping. It just shows if block is touching with any other canvas object.
overlapping_object=canvas.find_overlapping(block), overlapping_object could be a list that shows tags of objects that is block touching with.
How to make overlapping_object=canvas.find_overlapping(block) but it's correct. This one I typed here is just how could it look.
Thanks for any help!
I'm making 2D minecraft in tkinter and this is the thing that can really speed up my process.
If you're not willing to use other libraries and only tkinter, I don't think there's a built in function in tkinter that will allow it. I am also not sure why everything must be done in tkinter as it's not at all unusual for programs to use multiple libraries. Personally, given tikinter's limitations, I would use pygame to track polygons and their intersection but would never draw them. Short of using a third library (tikinter, python default, and other), there is one other approach. I mean it's really the only appraoch.
good ole fashioned math.
https://algs4.cs.princeton.edu/93intersection/
Here's some documentation on how to go about doing that. I wish you luck.
EDIT:
I think I accidentally found the answer to your question using math while studying for some other stuff. Still no way IN tkinter but here's a math explanation.
circle with center (x,y) and radius r
Polygon with z number of sides with x*2-1 number of points(x,y)
If you take iterate the lines of the polygon and put them through the following maths
Line J (x1,y1)(x2,y2)
m of Line J = (y1-y2)/(x1-x2)
create line P from circle center to P1 of LineJ
create line O from circle center to P2 of LineJ
Now we have a triangle
take the inverse cosine and length of P and O to get angle of the triangle you just made.
Make a right triangle by bisecting the triangle with line K starting at circle center and going out at the angle you just calculated.
Now you have line P and 1/2 angle of line P to line K
Now to find the intercept of that mid angle line
Tan(1/2 angle) = slope or m of the new line
using the x,y of the circle center calculate the slope intercept formula y=mx+b and get b
Now take the slope intercept formula for line J and set it equal to the slope intercept of the new line
line J (mx + b) = y = line P (mx + b)
Solve for y
Then plug y in the slope intercept for either and solve for x.
Once you've done this you have 4 points. The three points of the triangle, the point that makes a perpendicular line to center of circle from line J.
If any 3 of those points to the center of the circle in distance is smaller than r, they overlap. If they are are all > r then they don't overlap.

OpenGL output distorted (Possibly not offset to center?)

I am trying to output a square, and am getting a rather distorted rhombus. Like so,
And though I can tell that this is in fact the cube I had intended, the cube is strangely distorted. In my own workings to create a simple 3D projection program, I found a similar problem when I lacked the offset of 2D points to the middle of the screen, however I know of no such way to inform OpenGL of this offset...
For anyone who may be wondering, my current camera object looks like [in python]:
class Camera:
def __init__(self,x,y,z,fov=45,zNear=0.1,zFar=50):
self.x,self.y,self.z = x,y,z
self.pitch,self.yaw,self.roll = 0,0,0
glMatrixMode(GL_PROJECTION)
gluPerspective(fov, 1, zNear, zFar)
def __goto__(self,x,y,z,pitch,yaw,roll):
print "loc:",x,y,z
print "ang:",pitch,yaw,roll
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glRotatef(pitch,1,0,0)
glRotatef(yaw,0,1,0)
glRotatef(roll,0,0,1)
glTranslatef(-x,-y,-z)
def __flushloc__(self):
self.__goto__(self.x,self.y,self.z,self.pitch,self.yaw,self.roll)
with a cube being rendered in the following manner:
class Cube:
def __init__(self,x,y,z,width):
self.vertices=[]
for x in [x,x+width]:
for y in [y,y+width]:
for z in [z,z+width]:
self.vertices.append((x,y,z))
self.faces = [
[0,1,3,2],
[4,5,7,6],
[0,2,6,4],
[1,3,7,5],
[0,1,5,4],
[2,3,7,6]]
def __render__(self):
glBegin(GL_QUADS)
for face in self.faces:
for vertex in face:
glVertex3fv(self.vertices[vertex])
glEnd()
Perhaps I should also mention that the given window is 400px by 400px, thence the aspect ratio is 1.
Two things:
I suspect the "distortion" you're seeing is likely a normal effect of the perspective projection matrix; a square in 3D space can render as a weird rhombus shape in 2D when perspective is applied. It's hard to tell in this case what the expected output should be, because you haven't included the coordinates of the camera and the cube. However, if you used an orthogonal projection (via gluOrtho2D), my guess is that it would come out to be a (rotated, translated, and squashed) parallelogram.
Second, I think you're only seeing a single face of your cube displayed. The picture might make more sense if you could see the other visible faces of the cube, but your vertices list is getting trashed because you've reused the x,y,z variables in Cube.__init__.
If you fix this name collision, does your render get better? You might try renaming one set of x,y,z to something else, like this:
def __init__(self,x,y,z,width):
self.vertices=[]
for xv in [x,x+width]:
for yv in [y,y+width]:
for zv in [z,z+width]:
self.vertices.append((xv,yv,zv))

Python Graphics: Shape with function?

This assignment is asking me to draw a star function with four parameters.
"center point of the star
size of the star
color of the lines of the star
window used to draw the star"
This is the example picture given: http://i.stack.imgur.com/urvt2.jpg
The hint: given the center point, you can clone it and move it to create each of the 5 points (ex:p1 = cenpt.clone(), p1.move(0,-0.85*size))
I misinterpreted the prompt in the first attempt and so far, I've mostly hard coded.
import graphics
def main():
window= graphics.GraphWin("x", 600, 400)
center = graphics.Point(300, 200)
center.setFill("red")
center.draw(window)
p1 = center.clone()
p1.move(0,-110)
p1.setFill('red')
p1.draw(window)
p2 = p1.clone()
p2.move(150, 250)
p2.setFill('red')
p2.draw(window)
line1 = graphics.Line(p1,p2)
line1.setFill("black")
line1.draw(window)
window.getMouse()
main()
obviously this doesn't work for function purposes. How could I modify this to work for given parameters in a function?
If I were you, I'd define a function which given the coordinates and center points, would draw a point at the desired coordinates.
Something like this:
def makePoint (point, x, y):
newpoint = point.clone()
newpoint.move(x-300, y-200)
newpoint.setfill('red')
return newpoint
and then call that function once per point you want. If I wanted to use this to draw a point at coordinates 400, 500 I would call it like this: makePoint(center, 400, 500).draw(window)
You could also make one that just drew a new point at coordinates x and y instead of copying and moving a single point fairly easily, but it doesnt sound like thats what your instructor wants.

Drawing opposite lines in JES (Python)

So I have a random point on a canvas that will draw a line to a random point along a line across the bottom of the canvas. From that random point along the bottom of the canvas, I want to draw a line that diverges in the opposite direction (think of it like a " V ").
I'm having a serious issue conceptualizing what I need to do in order to accomplish getting the proper X coordinate for the second line that will be drawn (the Y-Coordinates will obviously be equal). I'm trying to do this using the addLine function in JES
If anyone could point me in the right direction I'd greatly appreciate it
It sounds like you basically want to just make an isosceles triangle here, so that the two legs of your V will be equal, though opposite in angle.
This doesn't define a strict angle between your two legs (which sounds like it's alright), but only that the x distance between both points and your point on the bottom of the canvas is equal.
Specifically, if you've got some code like this:
first_point = (a, b)
bottom_point = (c, d)
You want to make sure that the second_point is parallel though opposite to the first point, so the y coordinate should be the same, and the distance in the x-direction should be the same as the distance to the bottom_point, though in the opposite direction.
third_point_x = c - (a - c)
third_point = (third_point_x, b)
Hope that helps, let me know if you've got any more questions.

Implementing the local ridge orientation

I've been trying to implement the local ridge orientation for fingerprints in python. I've used the Gradient method, and using sobel operator to get the gradients I need. However it turned out that this method has quite a lot of flaws, especially around 90 degrees. I could include the code that I've done so far, but as it does not work as I want, I don't know if it's needed. I've also looked at the line segment method, however, I'm working with latent fingerprints so it is hard to know if one should look for maximum of black or white in the line segments. I've also tried to implement an algorithm to detect the area of maximum concentration of continous lines, but I couldn't get this to work. Any suggestion for other algorithms to use?
EDIT:
I'm using a function to apply my function to blocks, but that is hardly relevant
def lro(im_np):
orientsmoothsigma = 3
Gxx = cv2.Sobel(im_np,-1,2,0)
Gxy = cv2.Sobel(im_np,-1,1,1)
Gyy = cv2.Sobel(im_np,-1,0,2)
Gxx = scipy.ndimage.filters.gaussian_filter(Gxx, orientsmoothsigma)
Gxy = numpy.multiply(scipy.ndimage.filters.gaussian_filter(Gxy, orientsmoothsigma), 2.0)
Gyy = scipy.ndimage.filters.gaussian_filter(Gyy, orientsmoothsigma)
denom = numpy.sqrt(numpy.add(numpy.power(Gxy,2), (numpy.power(numpy.subtract(Gxx,Gyy),2))))# + eps;
sin2theta = numpy.divide(Gxy,denom) # Sine and cosine of doubled angles
cos2theta = numpy.divide(numpy.subtract(Gxx,Gyy),denom)
sze = math.floor(6*orientsmoothsigma);
if not sze%2: sze = sze+1
cos2theta = scipy.ndimage.filters.gaussian_filter(cos2theta, orientsmoothsigma) # Smoothed sine and cosine of
sin2theta = scipy.ndimage.filters.gaussian_filter(sin2theta, orientsmoothsigma)#filter2(f, sin2theta); # doubled angles
orientim = math.pi/2. + numpy.divide(numpy.arctan2(sin2theta,cos2theta),2.)
return orientim
I worked on this a long time ago, and wrote a paper on it. As I remember, look for both black and white ridges (invert the image and repeat the analysis) to give more results. I do remember some sensitivity at some angles. You probably need something with more extent than a pure Sobel. Try to reach out as many pixels as practical.
You may want to have a look at the work of Raymond Thai (Fingerprint Image
Enhancement and Minutiae Extraction), if you haven't already.

Categories

Resources