如何在一个圆圈中交替显示颜色,使圆圈看起来像旋转的? [英] how to alternate colors in a circle, so that circle looks like rotating?

查看:104
本文介绍了如何在一个圆圈中交替显示颜色,使圆圈看起来像旋转的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

预期的输出应该像这样,并且颜色也会改变其位置:

The expected output should be like this with the colors changing their position as well:

预期输出-:

颜色应该改变它们在圆中的位置,以便看起来好像它们在移动而不改变圆的位置.

the colors should change their positions in a circle so that it looks like they are moving without changing the position of circle.

尽管我的代码是用c/c ++的代码块编写的,但我很乐意获得其他任何编程语言的答案.

though my code is written in codeblocks in c/c++, i will be happy to get answers in any other programming languages.

我现在的代码

#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<iostream>

using namespace std;
void vvcircle(float xk,float yk,float radius);


int i=0;

int main()
{
float xk,yk,radius;

    int gdriver=DETECT,gmode,errorcode;
    initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI");
   // cout<<"enter the value of x, y and radius of circle"<<endl;
    //cin>>xk>>yk>>radius;


    vvcircle(200,200,100);

    getch();
    closegraph();
    return 0;
}

void vvcircle(float xk,float yk,float radius)
{
    int color[60]={0,1,2,3,4,5,6,7,8,9};
    while(radius>0)
    {

    float xo,yo;
    float P;
    xo=0.0;
    yo=radius;
P=1-radius;


///   vvcircle(200,200,100);

for(;xo<=yo;)
{



     putpixel(xo+xk,yo+yk,1);

     putpixel(yo+xk,xo+yk,1);


     putpixel(-yo+xk,xo+yk,2);


     putpixel(xo+xk,-yo+yk,2);


     putpixel(-yo+xk,-xo+yk,4);

          putpixel(-xo+xk,-yo+yk,4);

     putpixel(yo+xk,-xo+yk,4);

     putpixel(-xo+xk,+yo+yk,4);



    if(P<0)
    {

        xo=xo+1;
        yo=yo;
        P=P+2*xo+1;

    }
    else
    {

        xo=xo+1;
        yo=yo-1;
        P=P+(2*xo)-(2*yo)+1;
       //  putpixel(xo,yo,WHITE);

    }


}
radius=radius-1;

    }
}

当前输出-:

我得到许多带有颜色的同心圆.但是我想移动颜色,以便看起来像圆圈在移动,并且没有实现.

i get many concentric circles with colors. but i want to move the colors so that it looks like the circle is moving and it is not achieved.

推荐答案

这样的事情如何?

#include <math.h>
void my_circle(int xc,int yc,int r,float a) // center(x,y), radius, animation angle [rad]
    {
    const int n=4;          // segments count
    int x,sx,xx,x0,x1,rr=r*r,
        y,sy,yy,y0,y1,i,
        dx[n+1],dy[n+1],    // segments edges direction vectors
        c[n]={5,1,2,3};     // segments colors
    float da=2.0*M_PI/float(n);
    // BBOX
    x0=xc-r; x1=xc+r;
    y0=yc-r; y1=yc+r;
    // compute segments
    for (i=0;i<=n;i++,a+=da)
        {
        dx[i]=100.0*cos(a);
        dy[i]=100.0*sin(a);
        }
    // all pixels in BBOX
    for (sx=x0,x=sx-xc;sx<=x1;sx++,x++){ xx=x*x;
     for (sy=y0,y=sy-yc;sy<=y1;sy++,y++){ yy=y*y;
        // outside circle?
        if (xx+yy>rr) continue;
        // compute segment
        for (i=0;i<n;i++)
         if ((x*dy[i  ])-(y*dx[i  ])>=0)
          if ((x*dy[i+1])-(y*dx[i+1])<=0)
           break;
        // render
        putpixel(sx,sy,c[i]);
        }}
    }

它简单地遍历圆角正方形的所有像素到您的圆,确定像素是否在内部,然后检测它在哪个段中,并使用段颜色对其进行着色.

It simply loop through all pixels of outscribed square to your circle, determines if pixel is inside and then detect which segment it is in and color it with segments color.

通过从圆心到线段边缘的方向矢量描述线段.因此,如果像素在内部,则意味着其CW到一个边缘,而CCW到另一个边缘,因此在2D中检查矢量到像素和矢量到边缘之间的叉积的z坐标将告诉您像素是否在...

The segments are described by direction vectors from circle center towards the segments edges. So if pixel is inside it mean its CW to one edge and CCW to the other so in 2D inspecting z coordinate of the cross product between vector to pixel and vectors to edges will tell if the pixel is in or not ...

如您所见,我没有在自身渲染中使用浮点数学运算,只需要在渲染之前计算线段边缘矢量即可.

As you can see I did not use floating point math in the rendering it self, its needed only to compute the segments edge vectors prior rendering...

我使用了标准的256色VGA调色板(不确定BGI使用的是什么,我希望16色),因此在您的平台上,这里的颜色可能会有所不同:

I used standard 256 color VGA palette (not sure what BGI uses I expect 16 col) so the colors might be different on your platform here preview:

噪声是由于我的GIF捕获工具抖动了渲染本身是干净的而引起的...

The noise is caused by my GIF capturing tool dithering the render itself is clean ...

别忘了随着角度的变化反复调用 my_circle ...

Do not forget to call the my_circle repeatedly with changing angle ...

PS.我在没有BGI的BDS2006中对此进行了编码,因此在不同的编译器中,可能存在一些与所使用的语言怪癖有关的小语法问题...

PS. I encoded this in BDS2006 without BGI so in different compiler there might be some minor syntax problem related to used language quirks...

我以此伪造了 putpixel :

void putpixel(int x,int y,BYTE c)
    {
    static const DWORD pal[256]=
        {
        0x00000000,0x000000A8,0x0000A800,0x0000A8A8,0x00A80000,0x00A800A8,0x00A85400,0x00A8A8A8,
        0x00545454,0x005454FC,0x0054FC54,0x0054FCFC,0x00FC5454,0x00FC54FC,0x00FCFC54,0x00FCFCFC,
        0x00000000,0x00101010,0x00202020,0x00343434,0x00444444,0x00545454,0x00646464,0x00747474,
        0x00888888,0x00989898,0x00A8A8A8,0x00B8B8B8,0x00C8C8C8,0x00DCDCDC,0x00ECECEC,0x00FCFCFC,
        0x000000FC,0x004000FC,0x008000FC,0x00BC00FC,0x00FC00FC,0x00FC00BC,0x00FC0080,0x00FC0040,
        0x00FC0000,0x00FC4000,0x00FC8000,0x00FCBC00,0x00FCFC00,0x00BCFC00,0x0080FC00,0x0040FC00,
        0x0000FC00,0x0000FC40,0x0000FC80,0x0000FCBC,0x0000FCFC,0x0000BCFC,0x000080FC,0x000040FC,
        0x008080FC,0x009C80FC,0x00BC80FC,0x00DC80FC,0x00FC80FC,0x00FC80DC,0x00FC80BC,0x00FC809C,
        0x00FC8080,0x00FC9C80,0x00FCBC80,0x00FCDC80,0x00FCFC80,0x00DCFC80,0x00BCFC80,0x009CFC80,
        0x0080FC80,0x0080FC9C,0x0080FCBC,0x0080FCDC,0x0080FCFC,0x0080DCFC,0x0080BCFC,0x00809CFC,
        0x00B8B8FC,0x00C8B8FC,0x00DCB8FC,0x00ECB8FC,0x00FCB8FC,0x00FCB8EC,0x00FCB8DC,0x00FCB8C8,
        0x00FCB8B8,0x00FCC8B8,0x00FCDCB8,0x00FCECB8,0x00FCFCB8,0x00ECFCB8,0x00DCFCB8,0x00C8FCB8,
        0x00B8FCB8,0x00B8FCC8,0x00B8FCDC,0x00B8FCEC,0x00B8FCFC,0x00B8ECFC,0x00B8DCFC,0x00B8C8FC,
        0x00000070,0x001C0070,0x00380070,0x00540070,0x00700070,0x00700054,0x00700038,0x0070001C,
        0x00700000,0x00701C00,0x00703800,0x00705400,0x00707000,0x00547000,0x00387000,0x001C7000,
        0x00007000,0x0000701C,0x00007038,0x00007054,0x00007070,0x00005470,0x00003870,0x00001C70,
        0x00383870,0x00443870,0x00543870,0x00603870,0x00703870,0x00703860,0x00703854,0x00703844,
        0x00703838,0x00704438,0x00705438,0x00706038,0x00707038,0x00607038,0x00547038,0x00447038,
        0x00387038,0x00387044,0x00387054,0x00387060,0x00387070,0x00386070,0x00385470,0x00384470,
        0x00505070,0x00585070,0x00605070,0x00685070,0x00705070,0x00705068,0x00705060,0x00705058,
        0x00705050,0x00705850,0x00706050,0x00706850,0x00707050,0x00687050,0x00607050,0x00587050,
        0x00507050,0x00507058,0x00507060,0x00507068,0x00507070,0x00506870,0x00506070,0x00505870,
        0x00000040,0x00100040,0x00200040,0x00300040,0x00400040,0x00400030,0x00400020,0x00400010,
        0x00400000,0x00401000,0x00402000,0x00403000,0x00404000,0x00304000,0x00204000,0x00104000,
        0x00004000,0x00004010,0x00004020,0x00004030,0x00004040,0x00003040,0x00002040,0x00001040,
        0x00202040,0x00282040,0x00302040,0x00382040,0x00402040,0x00402038,0x00402030,0x00402028,
        0x00402020,0x00402820,0x00403020,0x00403820,0x00404020,0x00384020,0x00304020,0x00284020,
        0x00204020,0x00204028,0x00204030,0x00204038,0x00204040,0x00203840,0x00203040,0x00202840,
        0x002C2C40,0x00302C40,0x00342C40,0x003C2C40,0x00402C40,0x00402C3C,0x00402C34,0x00402C30,
        0x00402C2C,0x0040302C,0x0040342C,0x00403C2C,0x0040402C,0x003C402C,0x0034402C,0x0030402C,
        0x002C402C,0x002C4030,0x002C4034,0x002C403C,0x002C4040,0x002C3C40,0x002C3440,0x002C3040,
        0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,
        };
    if ((x<0)||(x>=Main->xs)) return;
    if ((y<0)||(y>=Main->ys)) return;
    Main->pyx[y][x]=pal[c];
    }

其中 Main-> xs,Main-> ys 是我的窗口分辨率,而 Main-> pyx 是其画布上的直接像素访问以获取更多信息,请参见:

Where Main->xs, Main->ys is my window resolution and Main->pyx is direct pixel acces to its canvas for more info see:

这篇关于如何在一个圆圈中交替显示颜色,使圆圈看起来像旋转的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆