drawing a chainsaw in ios core graphics - ios

I am trying to draw a chainsaw (just the blade) with ios core graphics, but getting stuck at a point, so far I've drawn something like this:
It looks ugly but I was just trying to see if I can draw one, and then do proper finishing later. The issue is that I can draw the teeth on the top and bottom flat sides but I have no idea how to draw the teeth around the curved corners. I've drawn the ellipse myself so I know where the coordinates are for the flat surfaces, but I don't know how to calculate the round corners. My questions are:
Is there an easy way to draw the teeth on the rounded corners ?
Is there a totally different and much better way to draw something like this in core graphics ?
The last question is regarding animating the chainsaw. I was hoping that if I can finish the drawing, then I can use a timer and redraw the teeth again with an offset, and then alternate between the two drawings to give a moving effect. Would that be the right way to go, or is it not worth doing such animation using core graphics and using something like an animated gif would be a better way ?
I am new to core graphics so don't know much details. I can imagine that there are multiple ways to achieve what I am doing, but what I mean when I say "is it the right way to do this" is it one of the right ways to do this, or I am going down a completely wrong path. Thanks !

(1) Drawing teeth around the rounded corner is a matter of identifying the points of the teeth. Consider that the inner-facing points of three teeth will fall along the rounded corner at angles: 1/8pi, 3/8pi, 5/8pi and 7/8pi.
The outer-facing points of those same three teeth will fall on a circle concentric to the rounded corner with a larger radius (larger by the height of a tooth). Those will fall at 1/4pi, 1/2pi and 3/4pi. The same idea can be reflected to the x<0 rounded corner on the left side. (or maybe not, maybe that side will be the chain saw's rectangular motor).
(2) I can't think of a totally different, much better way to do the drawing, except to point out that it could be done more realistically with an image (at least the static part).
(3) Probably wouldn't use a timer explicitly. I think the right way to go would be to place the chain on it's own CAShapeLayer. Have two (or more) chain paths (offset by some small phase shift in the placement of the teeth points). Add a repeating CABasicAnimation to the layer which alternates between the two paths.

Related

Find best rectangular fit for segmented contour

After segmentation of Objects in noisy data, I need to fit the best possible retangulat fit.
currently I just use opencv findContours and minAreaRect which will give me all around. I know that those objects will always be horizontal in the image with a maximum small angle like in this image.
This can be seen as the green rectanlge in the images, however I would need something like the red drawn rectangles, or even just the middle line (blue) since thats what I do need in the end.
Further, I also do have some conjunctions, like seen in this image:
Here I want to only detect the horizontal part and maybe know that there could be a junction.
Any idea how to solve this problem? I need some fast approach and have not found anything feasable yet.
Got much better results using distance transform (as mentioned from #Micka) on the masked Image, afterwars find the Line with the biggest distance as the middle of the rectangle (using some Filters, cuting off the curve) and in the End fitting a Line on the middle estimate.

Vector Graphics draw a knobs

I would like to draw an interface with knob, similar to "overdrive" (green) in this photo:
In iOS, such as vector graphics should I use? Quartz, OpenGL ES, or something else?
I'm sure can be done with OpenGL, but I think it's very complicated. So if you can I would avoid it using something more "simple."
It depends to a large extent how the rest of your GUI is rendered. However, unless you're already using OpenGL, Quartz or CoreAnimation are probably your best bet.
Looking at the screenshot, it seems you could probably achieve the effect with two image layers, a background (static) and a foreground (rotating).
The background image could have the scale (painted on the pedal) and shadow, then the black knob border and shiny metal middle. Then you can just draw the black tick mark indicator at the appropriate angle, either using Quartz or using a CGLayer and rotating it (especially if you wanted to have part of the button texture rotating with it).
It looks like the knob is smooth, so you don't need to worry about rotating the edges. And assuming the light source is fixed, the highlight on the top-left edge and the shine on the metal middle can be static too.
However, if you wanted to be more realistic, you could try having a third layer with just the shiny middle, and rotating this back and forth slightly to animate the knob middle as the pointer rotates. It doesn't need to go around all the way; maybe 10 degrees or so of variation should help sell the effect.

Matching dynamically drawn triangles and differentiating angles

I am making a game wherein the user draws triangles on a grid and be congruent with other triangles. However, the user gets additional points for having their new triangle in a different rotation from the original. I would use the rotation property of the movieclip, but since the triangles are drawn into a dynamically created MC, they all have a rotation of 0 degrees.
Is there some way to do this? I am absolutely stumped.
I think this is just a maths problem.
Firstly, if you have an equilateral triangle, you wouldn't reliably be able to work out the rotational difference since the sides are the same size.
Otherwise, you will always have 'the important side'
Assuming your triangle is isosceles, your important side is the one that is of a different length to the other two matching sides.
Assuming you have a scalene triangle, your most important side is the longest side.
Once you know your most important side...
You should be able to work out the important side of the users triangle using trig.
You should also know the important side of the base triangle the user is trying to draw against, since you are 'making' it.
Then you basically have two lines (the two important sides), use trig again to work out the difference in rotation between the two lines, then you are good to go.
I solved this. What I did was have the program dig through the triangle to find the left-most and uppermost point. Then I draw all the triangles using this point as the origin. This ensures that regardless of the order in which the dots are clicked, all triangles will have the same point of origin.
To detect whether they are matches, I wrote up a function that copies the triangles and moves them to the same point. Because they now have the same origin point, they will occupy the same space if they are of the same angle. Using this, I then wrote a function that checks to see if the triangles overlap completely.

rendering a photoshop style brush in openGL

I have lines that are programmatically defined by my program. what I want to do is render a brush stroke along them.
the way I think the type of brush I want works is, it simply has a texture, mostly transparent, and what you do is, render this texture centered on EVERY PIXEL in the path, and they blend together to create the stroke.
now assuming this even works, I'm going to make a bet that it will be WAY too expensive (targeting the ipad and other mobile chips, which HATE fillrate and alpha blending)
so, what other options are there?
if it could be done in realtime (that is, the path spline updating every frame) that would be ideal. but if not, within a fraction of a second on the ipad would be good too (the splines connect nodes, the user can drag nodes around thus transforming the spline, but it would be acceptable to revert to a simpler fill for the spline while it was moving around, then recalculate the brush once they release it)
for those wondering, I'm trying to get it so the thick lines look like they have been made with a pencil. it should look as real life as possible.
I considered just rendering the brushed spline to a texture, but as the spline can be any length, in any direction, dedicating a WHOLE rectangular texture to encompass the whole spline would be way to costly...
the spline is inevitably broken up into quads for rendering, so I thought of initially rendering the brush to a texture, then generating an optimized texture with each of the quads separated and packed as neatly as possible into the texture.
but two renders to texture... algorithm to create the optimized texture, making it so the quads still seamlessly blend with each other... sounds like a nightmare, and thats not even making it realtime.
so yeah, any ideas on how to draw thick, pencil like lines that follow a spline in real time on the ipad in openGL?
From my point of view, what you want is to render a line that:
is textured
has the edges fade off (i.e. no sharp edge to it)
follows a spline
To achieve these goals I would first of all break the spline up into a series of line segments that closely approximate the curve (you can make it more or less fine-grained depending on how accurate you want it to be versus how fast you want it to render).
Once you have these, you will need to make each segment into 3 quads, one that goes over the middle of the line segment that serves as the fully opaque part of the line and one on each edge of the line that will fade out to be totally transparent.
You will need to use a little bit of math to make sure that you extrude the quads along a vector that bisects 2 segments equally (i.e. so that the angle between the each segment and the extrusion vector are equal). This will ensure that you don't have gaps in the obtuse part of the join and overlaps in the acute parts.
After all of this, you just need to use the vertex positions as the UV co-ordinates (probably scaled though) and allow the texture to wrap around.
Using this method, you should end up with a mesh that has a solid thick line running through the middle of your spline with "fins" that taper off into complete transparency. This should approximate the effect you want quite closely while only rendering to relevant pixels (i.e. no giant areas of completely transparent pixels) and with very litter memory overhead.
I've been a little vague here as its kind of hard to explain with text alone and without writing an in depth tutorial. If you need more info, just comment on what your stuck on and I'll elaborate further.

Rendering a point light using 6 spot lights?

I'm trying to render 6 spot lights to create a point light for a shadow mapping algorithm.
I'm not sure if I'm doing this right, I've more or less followed the instructions here when setting up my view and projection matrices but the end result looks like this:
White areas are parts which are covered by one of the 6 shadow maps, the darker areas are ones which aren't covered by the shadowmaps. Obviously I don't have a problem with the teapots and boxes having their shadows projected onto the scene, however as you can see the 6 shadow maps have blindspots. Is this how a cubed shadow map is supposed to look? It doesn't look like a shadowmap of a point light source...
Actually you can adjust your six spots to have cones that perfectly fill each face of your cubemap. You can achieve this by setting each cone's aperture to create a circumscribed circle around each cubemap face. In this case you don't have to worry about overlapping, since the would be overlapping parts are out of the faces' area.
In other terms: adjust the lights' projection matrix' FOV, so it won't the view frustum that includes the light cone, but the cone will include the view frustum.
The a whole implementation see this paper.
What you're seeing here are a circle and two hyperbolas -- conic sections -- exactly the result you might expect if you took a double ended cone and intersected it with a plane.
This math may seem removed from the situation but it explains your problem. A spotlight creates a cone of light, and you can't entirely fill a solid space with a bunch of cones coming from the same point. (I'd suggest rolling up a bunch of pieces of paper and taping them together at the points to try it out.)
However, as you get far from the origin of your simulated-point-source, the cones converge to their assymptotes, and there is an infinitesimally-narrow gap in the light.
One option to solve this is to change the focus of the cones so that they overlap slightly -- this will create areas that are overexposed, but the overexposure will only become obvious as you get farther away. So long as all of your objects are near the point light source, this might not be much of an issue.
Another option is to move the focus of all of the lights much closer to their sources. This way, they'd converge to their assymptotes more quickly.

Resources