Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by z3nth10n · Dec 09, 2013 at 01:43 PM · guicirclefill

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:

Weird circle fill

It fills in a weird way...

so what can I do?

Thanks in advance. Bye.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image z3nth10n · Dec 09, 2013 at 08:25 PM 0
Share

Thanks a lot, I don't want open another topic for that... How can I make an Annulus and a Circular sector?? Thanks!!

avatar image Owen-Reynolds · Dec 09, 2013 at 10:19 PM 0
Share

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.

avatar image z3nth10n · Dec 10, 2013 at 06:20 PM 0
Share

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

avatar image Owen-Reynolds · Dec 11, 2013 at 04:40 PM 0
Share

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.

avatar image z3nth10n · Dec 12, 2013 at 06:07 PM 0
Share

Well I don't know why? But my circle's limit is 180 degrees as you can see there: Circle's 180 degree limit

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)
   }
 
 }


Show more comments
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image z3nth10n · Dec 09, 2013 at 02:44 PM 0
Share

It's too much for me, I hardly understand the function, can you help me? Thanks a lot!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Fill The Inside of a Prefab Circle in Unity 2D 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges