I am running clipper_console_demo.cpp compiled to a.out with following arguments:
Subject.txt:
50 50,
Clip.txt:
100 100,
100 0,
0 0,
0 100,
./a.out Subject.txt Clip.txt INTERSECTION
However the output 'solution.svg' is empty (there is only an outline of clipping polygon but not the subject). I also tried to change the orientation of the clipping square but solution is also empty.
Is it not possible to compute point/polygon intersection?
If not, what other combinations of subject/clip are not possible? As I understood from the doc, the clip has to be closed, but there are no other restrictions.
Related
How to draw rectangle with 1 rounded corner and fill it with color please?
I am trying to use the method arcTo with the following code:
this.bgGraphics.beginFill(0xFFCC00, 1);
this.bgGraphics.moveTo(0, 0);
this.bgGraphics.lineTo(45, 0);
this.bgGraphics.arcTo(45, 0, 60, 15, 15);
this.bgGraphics.lineTo(60, 60);
this.bgGraphics.lineTo(0, 60);
this.bgGraphics.endFill();
I.e. I am drawing a 60 x 60 rectangle and then trying to use arcTo from point 45, 0 to 45, 15 with radius 15.
But instead of the rounded corner on the right top it cuts it off:
The arcTo() method is a bit confusing. The (x1,y1) coordinates is not the start point of the curve. Think of it more like points for the bezier handles. In order to get the arc you want, you need to pull a bezier handle straight along the x axis. So your method should actually look like this:
this.bgGraphics.arcTo(60, 0, 60, 15, 15);
Since it's all one color, how about drawing a rounded rect with Graphics.drawRoundedRect and then drawing over the rounded parts that you don't want? You would draw a rounded rect the full size, and then cover up the corners that you want square with normal rects, like this:
I agree with Karmacon. I just wanted to add that sometimes it's easier to
use quadraticCurveTo(), as it has fewer options. You specify the Bezier control point x and y, and the end point x and y. However, you don't get the convenience of a radius parameter.
this.bgGraphics.quadraticCurveTo(60, 0, 60, 15);
Here's a comparison:
- arcTo(x1,y1,x2,y2,r);
x1 The x-coordinate of the first tangent
y1 The y-coordinate of the first tangent
x2 The x-coordinate of the second tangent
y2 The y-coordinate of the second tangent
r The radius of the arc
- quadraticCurveTo(cpx,cpy,x,y);
cpx The x-coordinate of the Bézier control point
cpy The y-coordinate of the Bézier control point
x The x-coordinate of the ending point
y The y-coordinate of the ending point
It's hugely useful to see images of the above but I can't post them yet. Have a look on W3Schools or developer.mozilla.org for some good images of how the parameters work.
I would like to animate an SVG stroke color change from say, red to green.
Is this possible?
I have managed to do so with the "fill" property, but for some reason i cannot do it with stroke.
One solution would be something along the lines of:
.velocity({
strokeRed: 0,
strokeGreen: 255,
strokeBlue: 0
});
My understanding is that linear 1s should the default timing, perhaps adding a named easing will enable that,
.velocity({
strokeRed: 0,
strokeGreen: 255,
strokeBlue: 0
}, "easeInSine");
Also, you can simply use "stroke", but note that stroke requires a hex value, unlike those listed above which can either be unitless or a percent.
So with RGBA, you set the alpha with the last attribute. However, I have seen people set the alpha as 255 to get it at 100% when I always thought the correct way was to set it to 1?
What I'm trying to say is this:
rgba(0, 0, 0, 120) // What they did
rgba(0, 0, 0, 0.47) // What I would do.
From what I can see, they do the same thing. Is there a "correct" way of doing it?
The type we usually use to represent the RGBA information should be an 4 bytes integer. 0x00000000 these four bytes represent red green blue alpha (or red blue green alpha) respectively.
So the "Correct" way is to set the last byte to be 255, if you want the alpha to be 1.
However, in some libraries or languages there are 2 interfaces to set the last byte to be 255:
rgba(int,int,int,int)
and
rgba(int,int,int,float)
So, that depends!
You should find out the interface in the documentation!
Hope this can help you!
I'm developing an application where users draw euclidean constructions on the HTML5 canvas. As such I can't really limit the size of certain shapes. When exaploring very large circles being drawn on the screen I noticed that very large circles don't have a constant radius.
To be more specific, a circle defined by two points, a center point and one specifing the radius doesn't pass throught the radius point anymore!
Progressivly larger circles. These are all supposed to pass through point E.
The error doesn't occure on multiples of 45 degrees = PI/4. Between these multiples the error is biggest (PI/8 for example)
Here is a jsfiddle containing the first example above:
http://jsfiddle.net/D28J2/2/
My questions: Why does this occure? and Is there some way to (efficently) work around this?
The way I worked around this issue completely was roll my own implementation of a circle draw approximation with bezier curves. An article detailing the implementation can be found here http://www.tinaja.com/glib/ellipse4.pdf.
function magic_circle(ctx, x, y, r){
m = 0.551784
ctx.save()
ctx.translate(x, y)
ctx.scale(r, r)
ctx.beginPath()
ctx.moveTo(1, 0)
ctx.bezierCurveTo(1, -m, m, -1, 0, -1)
ctx.bezierCurveTo(-m, -1, -1, -m, -1, 0)
ctx.bezierCurveTo(-1, m, -m, 1, 0, 1)
ctx.bezierCurveTo( m, 1, 1, m, 1, 0)
ctx.closePath()
ctx.restore()
}
With just these four segements I was able to approximate a circle much better then the build in google chrome canvas implementation.
This is probably a floating point cutoff error. Possibly because sine and cosine aren't giving perfectly accurate values. You can get around it (in Chrome at least) by rotating the canvas instead of the arc.
ctx.save(); // Save the canvas so we can rotate back.
ctx.translate(x, y); // Translate to the origin point.
ctx.rotate(alpha); // Rotate the proper angle.
ctx.arc(0, 0, 3, 0, Math.PI*2); // Draw the small circle at the origin.
ctx.fill();
ctx.arc(r, 0, r, 0, Math.PI*2); // Create a big with the origin 1 radius away.
ctx.restore(); // Restore the canvas to the original orientation
// before drawing. Otherwise the circle looks bad.
ctx.strokeStyle = "black";
ctx.stroke(); // Draw!
I am a big fan of manipulating the canvas instead of shapes. It gives you a more logical area to work with. See http://jsfiddle.net/D28J2/10/
Just throwing this out there but could it be an issue without specifying enough digits of PI? Whenever I do things like that I tend to go slightly overboard and use about 10 digits of PI.
In Google chrome I can repeat the issue but in IE 9 and IE 10 all is fine.
So my guess is that the implementation is wrong in Chrome. It might be a rounding error or they used an interpolation method to avoid sin and cos which isn't very acurate.
Look here as well: HTML5 canvas arcs not rendering correctly in Google Chrome
The only work around I can imagine is drawing the circle by your own code or using a (jQuery?) plug-in that does this for you.
http://jsfiddle.net/jBgqW/
I've painted the background with fillRect and fillStyle set to rgb(255,0,0) but when I iterate through the pixels and set some random color and value of the alpha pixel to 0 everything becomes white. I've assumed that when the pixel is transparent it should blend with the previously painted background color or does it always default to white.
I hope that it's just my wrong way of using the canvas.
Can anyone explain why the background isn't red in this case and how do i use the alpha pixel properly? I would like to know if this has something to do with the alpha premultiplication.
When using globalAlpha, the pixel colors are calculated with the current rgba values and the new values.
However, in this case you're setting the values manually and therefore doing no calculations. You're just setting the rgba values yourself, which means that the alpha channel is not used for calculating but is just altered without further use. The previous color (red) is basically overwritten in a 'brute force' way - instead of rgba(255, 0, 0, 255), it's now just rgba(128, 53, 82, 0). The original red color has simply been thrown away.
As a result, an alpha of 0 represents complete transparency, so you see the colors of the parent element.
This can be confirmed if you change the body background color: http://jsfiddle.net/jBgqW/2/.
This is somewhat thread necromancy, but I've just faced this problem and have a solution to it, if not for the original poster then for people like me coming from google.
As noted, putImageData directly replaces pixels rather than alpha blends, but that also means it preserves your alpha data. You can then redraw that image with alpha blending using drawImage.
To give an example, lets says we have a canvas that is 200 by 100 pixels and a 100 by 100 imageData object.
// our canvas
var canvas = document.getElementById("mycanvas");
var ctx = canvas.getContext("2d");
// our imageData, created in whatever fashion, with alpha as appropriate...
var data = /* ... */
// lets make the right half of our canvas blue
ctx.fillStyle="blue";
ctx.rect(100, 0, 100, 100);
ctx.fill();
// now draw our image data to the left (white) half, pixels are replaced
ctx.putImageData(data, 0, 0, 100, 100);
// now the magic, draw the canvas to itself with clipping
ctx.drawImage(canvas, 100, 0, 100, 100, 100, 0, 100, 100);
Voila. The right half of the image is now your image data blended with the blue background, rendered with hardware assistance.