- Home /
How to fill circle in Eric5h5's TextureDrawCircle function
Hi,
well I'm make a new version of that http://wiki.unity3d.com/index.php?title=TextureDrawCircle
function Circle (tex : Texture2D, cx : int, cy : int, r : int, col : Color) {
for(i = 0; i <= r; i++) {
var y = r - i;
var d = 1/4 - r;
var end = Mathf.Ceil(r/Mathf.Sqrt(2));
for (x = 0; x <= end; x++) {
tex.SetPixel(cx+x, cy+y, col);
tex.SetPixel(cx+x, cy-y, col);
tex.SetPixel(cx-x, cy+y, col);
tex.SetPixel(cx-x, cy-y, col);
tex.SetPixel(cx+y, cy+x, col);
tex.SetPixel(cx-y, cy+x, col);
tex.SetPixel(cx+y, cy-x, col);
tex.SetPixel(cx-y, cy-x, col);
d += 2*x+1;
if (d > 0) {
d += 2 - 2*y--;
}
}
}
}
I have a circumference, and I want to fill it for make a circle... I only have to substract one from the radius, and I will fill it, but:
It fills in a weird way...
so what can I do?
Thanks in advance. Bye.
Answer by Owen-Reynolds · Dec 09, 2013 at 05:00 PM
The original code is seem a clever method of very quickly drawing just an outline. Using it 50 times in a row loses the "very quickly" part, so may as well go back to brute force. Scan every pixel, compute distance from the center, and paint:
for(int x=0;i<256;x++)
for(int y=0;i<256;y++) {
float dx = x-cx, dy=y-cy;
float dist = Mathf.sqrt(dx*dx+dy*dy);
if(dist<rad) SetPixel(...);
// or if(dx*dx+dy*dy<rad*rad)
}
That allows you to make "thick" rings by checking between radius1 and radius2, or make ovals by scaling dx or dy. Or add some fade-out pixels with a small lerp if dist is just outside of the radius.
Thanks a lot, I don't want open another topic for that... How can I make an Annulus and a Circular sector?? Thanks!!
An annulus is just "thick rings" (last para, above.) A "pie graph" chunk is between two angles. To find the angle in the code above, use atan2(dy,dx)
(in radians, but can convert to degrees.)
I think if you want to make all sorts of special circular-like things, you'll want/need to review trigonometry. But once you do, you can plug standard math into the "test every pixel" loop.
Ok, but in "pie graphs" as you call it, I have to create a new float and assign it to the Sqrt function, or how? Can you give me an example, I'm very newbie with trigonometric functions... I don't have studied it in school yet... (I only have 15 years) So... :P
Perfect -- ask your math $$anonymous$$cher!
sqrt(x*x+y*y)
is the Pythagorean theorum for distance to (x,y). To get part of a circle, check radius exactly the same, but also check the angle. The angle of point (x,y) is the arc-tangent (also called the inverse tangent, or tan^-1 on calculators) which is in radians, counterclockwise (ouch.) To get 1/4th circle, check if(angle>=0 && angle<=1.57f && dist<=rad)
But there's not way I can $$anonymous$$ch you more trig in an internet comment than your $$anonymous$$cher can, or a good trig site.
Well I don't know why? But my circle's limit is 180 degrees as you can see there:
That is my code:
function Circle (tex : Texture2D, cx : int, cy : int, r : int, ang : int, cc : int, col : Color) {
for(x=0;x<256;x++)
for(y=0;y<256;y++) {
var dx : float = x-cx;
var dy : float = y-cy;
var dist : float = $$anonymous$$athf.Sqrt(dx*dx+dy*dy);
var angle : float = $$anonymous$$athf.Atan2(dx, dy);
var angulo : float = ang * 0.0174532925f;
var colornulo : Color = new Color(0,0,0,0);
if(angle>=0 && angle<=angulo && dist<r) { tex.SetPixel(x, y, col); } else { tex.SetPixel(x, y, colornulo); }
if(dist<cc) tex.SetPixel(x, y, colornulo);
//if(dist>cc) tex.SetPixel(x, y, colornulo);
// or if(dx*dx+dy*dy<rad*rad)
}
}
Answer by CHPedersen · Dec 09, 2013 at 02:22 PM
Nice. :) It looks like classical spatial aliasing artifacts to me. It occurs because you're trying to fill the circle by drawing concentric circles in pixel space from the center and outwards, one per radius, measured in pixels. But because pixels are inherently a discrete partitioning of the area of the circle, there will be pixels in its area to which no circumference of any of the concentric circles pass. These remain white, because none of the circles you've drawn ever map their circumferences to those pixels.
You can fix it by further modifying the algorithm so that it draws circles based on floating point radii instead of defining the radii in pixel space, and then draw more circles than there are pixel-radii. You could also try to increase the width of the lines drawn by those circles. If you can make them 2 px wide, they might cover those white pixels. Or you could run a filter over the portion of the image that contains the circle afterwards, and fill the gaps yourself.
It's too much for me, I hardly understand the function, can you help me? Thanks a lot!
Your answer
Follow this Question
Related Questions
Cooldown effect in GUI 0 Answers
Round Slider (Circle shaped slider GUI control) 1 Answer
Circle GUI Button, Javascript? 1 Answer
RenderTexture on GUI. 1 Answer