Two CGpaths - 1 CAAnimation - but the second path should be repeated - uibezierpath

If I have a path which starts at the top of the screen and eases down into an oval shape. I would like to once in the oval shape for the animation to keep repeating the oval shape. How do I do this with CAKeyframeAnimations? Do I have two and delay the second? Do I wait for the first to complete and then add the second? Or is there another way?
Here my path/s
//The oval shape
UIBezierPath *trackPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 100, 280, 150)];
//the curve from the top of the screen easing into the oval.
[trackPath moveToPoint:P(160, -80)];
[trackPath addCurveToPoint:P(297, 192)
controlPoint1:P(244, 30)
controlPoint2:P(316, 145)];

So I said to myself "Self.....it looks like you are going to have to answer this one yourself as nobody even wants to edit it or say that it is a duplicate......"
One answer seems to be to create an animation with the first path. When this animation is finished use the delegate method of CAAnimation to know this, then start the second animation with the new path. The second one of course will be on repeat.

Related

Why are the transparent pixels not blending correctly in WebGL

Result of my code:
Basically, what the issue is, the transparent part of my image are not blending correctly with what is drawn before it. I know I can do a
if(alpha<=0){discard;}
in the fragment shader, the only issue is I plan on having a ton of fragments and don't want the if statement for each fragment on mobile devices.
Here is my code related to alpha, and depth testing:
var gl = canvas.getContext("webgl2",
{
antialias : false,
alpha : false,
premultipliedAlpha: false,
}
);
gl.enable(gl.BLEND);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.GREATER);
Also, these are textured gl.POINTS I am drawing. If I change the order the two images are drawn in the buffer, the problem doesn't exist. They will be dynamically rotating during the program's runtime so this is not an option.
It's not clear what your issue is without more code but it looks like a depth test issue.
Assuming I understand correctly you're drawing 2 rectangles? If you draw the red one before the blue one then depending on how you have the depth test setup the blue one will fail the depth test when the X area is drawn.
You generally solve this by sorting what you draw, making sure to draw things further away first.
For a grid of "tiles" you can generally sort by walking the grid itself in the correct direction instead of "sorting"
On the other hand, if all of your transparency is 100% draw or not draw then discard has its advantages and you can draw front to back. The reason is because in that case drawing front to back, the pixel drawn (not discarded) by the red quad will be rejected when drawing the blue quad by the depth test. The depth test is usually optimized to happen before running the fragment shader for a certain pixel. If the depth test says the pixel will not be drawn then no reason to even run the fragment shader for that pixel, time saved. Unfortunately as soon as you have any transparency that is not 100% opaque or 100% transparent then you need to sort and draw back to front. Some of these issues are covered in this article
A few notes:
you mentioned mobile devices and you mentioned WebGL2 in your code sample. There is no WebGL2 on iOS
you said you're drawing with POINTS. The spec says only POINTS of 1 pixel in size are required. It looks like you're safe up to points of size 60 but to be safe it's generally best to draw with triangles as there are other isses with points
you might also be interested in sprites with depth

setting fixed height on Line using fabric.js and canvas

I am using fabric.js for creating a canvas project. I have created a line using the framework. However, is there a way to set a fixed length on the line. Reason being, the line is draggable and it stretches.
line = makeLine([ 250, 125, 250, 175 ]),
It got quite complicated in the comments so let's sum up few things:
Here's a solution for the simple case:
In your http://jsfiddle.net/sprakashg/rbhhP/ you have few lines like this:
var p = e.target;
p.line1 && p.line1.set({ 'x2': p.left, 'y2': p.top });
Here, the line1 is your Line and p is your Circle.
You can compute the initial length of the line and store it. Next, you can compute a current line direction, which is from line.p1 to position of p. Now, if you normalize this direction and multiply by the initial line length you'll get the new direction, pointing at your circle, but keeping initial line length. Add it to your line start and you can store this value in p.line1.set().
This will work if you have two circles connected with a line, but if you have few lines connected to the single circle, then you'll not be able to move it.
Solution for more complex case:
In your Stickman case you can't move a knee because there's only one another point that satisfies your lines length constraints. In such case I'd add a simple hierarchy to the model.
If you assume some Circle to be the root of the model, then all other objects are leaves and branches of some model tree. Now, when you move some circle, you should do the computations mentioned above just for the line, that connects it with the parent.
Then you can take all the circles below in your model hierarchy and move them by the same amount that you moved your main Circle.
With this approach, when you try to move a knee then your pelvis-knee line will move with your mouse but keep its length (as you want to). But during the movement of the knee the rest of a leg will move by the same amount, so in effect you'll also keep the length of a knee-ankle line.

OpenGL Vertical Flip: -360 + -360 = 0?

I am working with OpenGL and I wanted to invert the image. So I went here, asked a question and finally I had the following code:
glMatrixMode(GL_PROJECTION);
glScalef(-1,1,1);
glTranslatef(-width(),0,0);
From what I understand from this, the position of every pixel gets inverted, so the pixels that were on the right of the image are now on the same absolute position, but are the left of the image, so I have to move the entire thing back exactly as many pixels as its wide: 360 (which is the size of the "canvas", so in the snippents the function width() is being used)! So to undo this process I would invert the image again and then move it back to where it came from:
glMatrixMode(GL_PROJECTION);
glScalef(-1,1,1);
glTranslatef(width(),0,0);
Nope, blackscreen. I have to do exactly the same thing twice to undo the flipping: I have to move with -360 every time I flip the image. Why?
It's exactly as Daniel Fischer mentioned in the comment. Here is an illustration of the process.
What you must have in mind is that the transformations operate on the transformed coordinate systems.
We start with the image (grey) on the screen (green):
Then we scale the image. So the origin is preserved, but the x-axis is mirrored.
No we have to move the image onto the screen again. Because the x-axis points to the left (but we want to move the image to the right), we have to use a negative offset for the translation:
If we flip the image again, the following happens. The origin is preserved and the x-axis is mirrored:
So we must translate the image by a negative offset:
Another way of undoing the flip is undoing the operations (but in the opposite order):
glTranslatef(width, 0, 0);
glScalef(-1,1,1);
The mathematical reason for that is that inversion reverses the oder. If we have Matrix A = B * C then A^-1 = (C^-1 * B^-1).

How to write a Bezier curved path on the fly with one fixed point and one changing

If I have an object performing this CAKeyframe Animation path (it is just an oval shape in the upper region of an iphone..
UIBezierPath *trackPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 100, 280, 150)];
Now imagine that at anytime during this paths travels I want to create a smooth path from its current position at a point in time and some point at the bottom of the screen.
By the way I am assuming that once I have a path I can stop the current CAKeyframeAnimation and add this path to a new CAKeyframe animation, but maybe if this is incorrect you can give me pointers here as well please.
So I said to myself "Self.....it looks like you are going to have to answer this one yourself as nobody even wants to edit it or say that it is a duplicate......"
So I was hoping for some easier or ready made way to do this. From what I have found there is no easy way. It seems the steps are going to be;
get the current position of the animated layer
calculate yourself with all your own code a nice smooth curve
add this path to an animation and animate it.
As for the calculation of a Bezier Curve I am still looking to find some class or code where points can be plugged into it and the two control points are produced for the UIBezier class to produce a curve.
In my case I am going to only animate "out" of my above questioned shape a determined points and thus have ready made smooth curves which I have prepared.
Animating a smooth exit from an oval at any point is very complicated and In my case just not worth it. So I have not done that.

Where can I find information on line growing algorithms?

I'm doing some image processing, and I need to find some information on line growing algorithms - not sure if I'm using the right terminology here, so please call me out on this is needs be.
Imagine my input image is simply a circle on a black background. I'd basically like extract the coordinates, so that I may draw this circle elsewhere based on the coordinates.
Note: I am already using edge detection image filters, but I thought it best to explain with a simple example.
Basically what I'm looking to do is detect lines in an image, and store the result in a data type where by I have say a class called Line, and various different Point objects (containing X/Y coordinates).
class Line
{
Point points[];
}
class Point
{
int X, Y;
}
And this is how I'd like to use it...
Line line;
for each pixel in image
{
if pixel should be added to line
{
add pixel coordinates to line;
}
}
I have no idea how to approach this as you can probably establish, so pointers to any subject matter would be greatly appreciated.
I'm not sure if I'm interpreting you right, but the standard way is to use a Hough transform. It's a two step process:
From the given image, determine whether each pixel is an edge pixel (this process creates a new "binary" image). A standard way to do this is Canny edge-detection.
Using the binary image of edge pixels, apply the Hough transform. The basic idea is: for each edge pixel, compute all lines through it, and then take the lines that went through the most edge pixels.
Edit: apparently you're looking for the boundary. Here's how you do that.
Recall that the Canny edge detector actually gives you a gradient also (not just the magnitude). So if you pick an edge pixel and follow along (or against) that vector, you'll find the next edge pixel. Keep going until you don't hit an edge pixel anymore, and there's your boundary.
What you are talking about is not an easy problem! I have found that this website is very helpful in image processing: http://homepages.inf.ed.ac.uk/rbf/HIPR2/wksheets.htm
One thing to try is the Hough Transform, which detects shapes in an image. Mind you, it's not easy to figure out.
For edge detection, the best is Canny edge detection, also a non-trivial task to implement.
Assuming the following is true:
Your image contains a single shape on a background
You can determine which pixels are background and which pixels are the shape
You only want to grab the boundary of the outside of the shape (this excludes donut-like shapes where you want to trace the inside circle)
You can use a contour tracing algorithm such as the Moore-neighbour algorithm.
Steps:
Find an initial boundary pixel. To do this, start from the bottom-left corner of the image, travel all the way up and if you reach the top, start over at the bottom moving right one pixel and repeat, until you find a shape pixel. Make sure you keep track of the location of the pixel that you were at before you found the shape pixel.
Find the next boundary pixel. Travel clockwise around the last visited boundary pixel, starting from the background pixel you last visited before finding the current boundary pixel.
Repeat step 2 until you revisit first boundary pixel. Once you visit the first boundary pixel a second time, you've traced the entire boundary of the shape and can stop.
You could take a look at http://processing.org/ the project was created to teach the fundamentals of computer programming within a visual context. There is the language, based on java, and an IDE to make 'sketches' in. It is a very good package to quickly work with visual objects and has good examples of things like edge detection that would be useful to you.
Just to echo the answers above you want to do edge detection and Hough transform.
Note that a Hough transform for a circle is slightly tricky (you are solving for 3 parameters, x,y,radius) you might want to just use a library like openCV

Resources