Algorithm Generation flag

Source: Internet
Author: User

This article will provide a set of algorithms for generating flag, and the code is full of positive energy. The previous article was to generate an n-awn star, a pentagram is a kind of awn, so it is not difficult to generate a pentagram image. the flag of the People's Republic of China is flag, the flag face is red, the ratio of length to width is 3:2. Top left yellow Pentagram five, four small star ring arch on the right of a big star, and each has a corner point is the center of the Big star. 1, 4 yellow star aligned, 2, 3 yellow star aligned.

The code is as follows:

H

1 classCpixel5starsredflag: Publicipixelequation2 {3  Public:4 Cpixel5starsredflag ();5 6     Const Char* GetName ()Const7     {8         return "Five Stars Red Flag";9     }Ten  OneUnsignedintCalculatepixel (unsignedintX, unsignedinty); A  -     structStar -     { the         floatPosX; -         floatPosY; -         floatradius; -         floatangle; +     }; -     BOOLIspointinstar (floatXfloatY, star&star); +  A Private: at     floatm_list_sin[5]; -     floatm_list_cos[5]; -  -Star m_list_stars[5]; -};

The first article defines a structural star that determines the position, size, and angle of rotation of a pentagram. You need to set the appropriate value for each star, the code I provide is the parameters I have tried out many times, and does not exactly match the actual.

Cpp

1 Cpixel5starsredflag::cpixel5starsredflag ()2 {3M_width =1680;4M_height =1120;5 6      for(inti =0; I <5; i++)7     {8M_list_sin[i] = Sinf ((I-0.5f)*2*pi/5);9M_list_cos[i] = COSF ((I-0.5f)*2*pi/5);Ten     } One  Am_list_stars[0].POSX = m_width*0.2f; -m_list_stars[0].posy = m_height*0.35f; -m_list_stars[0].radius = m_height*0.1f; them_list_stars[0].angle =0.0f; -  -     floatr = m_list_stars[0].radius *3.0f; -  +m_list_stars[1].POSX = m_list_stars[0].posx + R*COSF (pi/4); -m_list_stars[1].posy = m_list_stars[0].posy + r*sinf (pi/4); +m_list_stars[1].radius = m_list_stars[0].radius*0.5f; Am_list_stars[1].angle =-pi/8; at  -m_list_stars[2].POSX = m_list_stars[0].posx + R*COSF (pi/ A); -m_list_stars[2].posy = m_list_stars[0].posy + r*sinf (pi/ A); -m_list_stars[2].radius = m_list_stars[0].radius*0.5f; -m_list_stars[2].angle =-pi/ -; -  inm_list_stars[3].POSX = m_list_stars[0].posx + R*COSF (-pi/ A); -m_list_stars[3].posy = m_list_stars[0].posy + r*sinf (-pi/ A); tom_list_stars[3].radius = m_list_stars[0].radius*0.5f; +m_list_stars[3].angle = pi/ -; -  them_list_stars[4].POSX = m_list_stars[0].posx + R*COSF (-pi/4); *m_list_stars[4].posy = m_list_stars[0].posy + r*sinf (-pi/4); $m_list_stars[4].radius = m_list_stars[0].radius*0.5f;Panax Notoginsengm_list_stars[4].angle = pi/8; - } the  + BOOLCpixel5starsredflag::ispointinstar (floatXfloatY, star&star) A { the     floati = x-Star.posx; +     floatj = y-Star.posy; -  $     if(I*i + j*j > star.radius*Star.radius) $     { -         return false; -     } the  -     float_s =Sinf (star.angle);Wuyi     float_c =COSF (star.angle); the  - Vec2 P; Wup.x = i*_c-j*_s; -P.Y = i*_s + j*_c; About  $VEC2 listpoints[5]; -      for(unsignedintm =0; M <5; m++) -     { -listpoints[m].x = star.radius*M_list_sin[m]; ALISTPOINTS[M].Y = star.radius*M_list_cos[m]; +     } the  -     BOOLBcenter =true; $      for(unsignedintm =0; M <5; m++) the     { thevec2& v =Listpoints[m]; thevec2& vL1 = listpoints[(M +4)%5]; thevec2& vL2 = listpoints[(M +3)%5]; -vec2& vR1 = listpoints[(M +1)%5]; invec2& vR2 = listpoints[(M +2)%5]; the  the         if(!Ispointinangle (vL2, V, vR2, P)) About         { theBcenter =false; the         } the         Else if(Issameside (vL1, vR1, V, P)) +         { -             return true; the         }Bayi     } the  the     returnBcenter; - } -  theUnsignedintCpixel5starsredflag::calculatepixel (unsignedintX, unsignedinty) the { theUnsignedintRed =0xffff0000; theUnsignedintYellow =0xfffff000; -  the     floati = (float) x; the     floatj = (float) y; the 94      for(unsignedintm =0; M <5; ++m) the     { the         if(Ispointinstar (i, J, M_list_stars[m])) the         {98             returnYellow; About         } -     }101 102     returnRed;103}

base class ipixelequation code See: Why Mathematical image generation tool

On the definition of structural VEC2 and its related functions see: The optimal algorithm for judging points in a triangle on two-dimensional plane

The code is not too elegant between the proportions of the graph, and the actual will be slightly inconsistent. The resulting image is as follows:

corresponding software:

Why Math image Generation tool

Related articles:

Algorithm generates blue sky everywhere red flag

The beauty of the algorithm---100 images generated by the program, there is always a piece that makes you feel amazing [top]

The beauty of the algorithm---100 images generated by the program, there is always a piece that makes you feel amazing [next]

Algorithm Generation flag

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.