Unity GL circle, unitygl circle

Source: Internet
Author: User

Unity GL circle, unitygl circle

In Unity, GL has no circular function and can only be used by itself.

I am also very happy to help you.

Although there is no circle function, but can draw a straight line, using this, with calculus or something, can be drawn. It is a straight line that spends a lot of connections. When each line is short enough, it will be round enough.

1 void DrawCircle (float x, float y, float z, float r, float accuracy) 2 {3 GL. pushMatrix (); 4 // draw 2D image 5 GL. loadOrtho (); 6 7 float stride = r * accuracy; 8 float size = 1/accuracy; 9 float x1 = x, x2 = x, y1 = 0, y2 = 0; 10 float x3 = x, x4 = x, y3 = 0, y4 = 0; 11 12 double squareDe; 13 squareDe = r * r-Math. pow (x-x1, 2); 14 squareDe = squareDe> 0? SquareDe: 0; 15 y1 = (float) (y + Math. sqrt (squareDe); 16 squareDe = r * r-Math. pow (x-x1, 2); 17 squareDe = squareDe> 0? SquareDe: 0; 18 y2 = (float) (y-Math. sqrt (squareDe); 19 for (int I = 0; I <size; I ++) {20x3 = x1 + stride; 21x4 = x2-stride; 22 squareDe = r * r-Math. pow (x-x3, 2); 23 squareDe = squareDe> 0? SquareDe: 0; 24 y3 = (float) (y + Math. sqrt (squareDe); 25 squareDe = r * r-Math. pow (x-x4, 2); 26 squareDe = squareDe> 0? SquareDe: 0; 27 y4 = (float) (y-Math. sqrt (squareDe); 28 29 // draw a line segment 30 GL. begin (GL. LINES); 31 GL. color (Color. blue); 32 GL. vertex (new Vector3 (x1/Screen. width, y1/Screen. height, z); 33 GL. vertex (new Vector3 (x3/Screen. width, y3/Screen. height, z); 34 GL. end (); 35 GL. begin (GL. LINES); 36 GL. color (Color. blue); 37 GL. vertex (new Vector3 (x2/Screen. width, y1/Screen. height, z); 38 GL. vertex (new Vector3 (x4/Screen. width, y3/Screen. height, z); 39 GL. end (); 40 GL. begin (GL. LINES); 41 GL. color (Color. blue); 42 GL. vertex (new Vector3 (x1/Screen. width, y2/Screen. height, z); 43 GL. vertex (new Vector3 (x3/Screen. width, y4/Screen. height, z); 44 GL. end (); 45 GL. begin (GL. LINES); 46 GL. color (Color. blue); 47 GL. vertex (new Vector3 (x2/Screen. width, y2/Screen. height, z); 48 GL. vertex (new Vector3 (x4/Screen. width, y4/Screen. height, z); 49 GL. end (); 50 51x1 = x3; 52x2 = x4; 53 y1 = y3; 54 y2 = y4; 55} 56 GL. popMatrix (); 57}

The parameters are: x, y, and z center three-dimensional coordinates, r circle radius, and accuracy. The smaller the precision, the more the circle

 

If any error occurs, please kindly advise

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.