# 代码分享：贝塞尔曲线【空间视频有讲解】

struct xy{
int x;
int y;

xy(){
}
xy(int m){
x=m;
y=m;
}
xy(int x,int y){
this->x=x;
this->y=y;
}

xy operator  *(xy&m){return xy(x *m.x,y *m.y);}
xy operator  /(xy&m){return xy(x /m.x,y /m.y);}
xy operator  %(xy&m){return xy(x %m.x,y %m.y);}
xy operator  +(xy&m){return xy(x +m.x,y +m.y);}
xy operator  -(xy&m){return xy(x -m.x,y -m.y);}
xy operator <<(xy&m){return xy(x<<m.x,y<<m.y);}
xy operator >>(xy&m){return xy(x>>m.x,y>>m.y);}
xy operator  &(xy&m){return xy(x &m.x,y &m.y);}
xy operator  |(xy&m){return xy(x |m.x,y |m.y);}
xy operator  ^(xy&m){return xy(x ^m.x,y ^m.y);}
xy operator  *=(xy&m){x *=m.x;y *=m.y;return *this;}
xy operator  /=(xy&m){x /=m.x;y /=m.y;return *this;}
xy operator  %=(xy&m){x %=m.x;y %=m.y;return *this;}
xy operator  +=(xy&m){x +=m.x;y +=m.y;return *this;}
xy operator  -=(xy&m){x -=m.x;y -=m.y;return *this;}
xy operator <<=(xy&m){x<<=m.x;y<<=m.y;return *this;}
xy operator >>=(xy&m){x>>=m.x;y>>=m.y;return *this;}
xy operator  &=(xy&m){x &=m.x;y &=m.y;return *this;}
xy operator  |=(xy&m){x |=m.x;y |=m.y;return *this;}
xy operator  ^=(xy&m){x ^=m.x;y ^=m.y;return *this;}
xy operator  *(int m){return xy(x *m,y *m);}
xy operator  /(int m){return xy(x /m,y /m);}
xy operator  %(int m){return xy(x %m,y %m);}
xy operator  +(int m){return xy(x +m,y +m);}
xy operator  -(int m){return xy(x -m,y -m);}
xy operator <<(int m){return xy(x<<m,y<<m);}
xy operator >>(int m){return xy(x>>m,y>>m);}
xy operator  &(int m){return xy(x &m,y &m);}
xy operator  |(int m){return xy(x |m,y |m);}
xy operator  ^(int m){return xy(x ^m,y ^m);}
xy operator  *=(int m){x *=m;y *=m;return *this;}
xy operator  /=(int m){x /=m;y /=m;return *this;}
xy operator  %=(int m){x %=m;y %=m;return *this;}
xy operator  +=(int m){x +=m;y +=m;return *this;}
xy operator  -=(int m){x -=m;y -=m;return *this;}
xy operator <<=(int m){x<<=m;y<<=m;return *this;}
xy operator >>=(int m){x>>=m;y>>=m;return *this;}
xy operator  &=(int m){x &=m;y &=m;return *this;}
xy operator  |=(int m){x |=m;y |=m;return *this;}
xy operator  ^=(int m){x ^=m;y ^=m;return *this;}
bool operator  <(xy&m){return((unsigned(x) <unsigned(m.x))&&(unsigned(y) <unsigned(m.y)));}
bool operator  >(xy&m){return((unsigned(x) >unsigned(m.x))&&(unsigned(y) >unsigned(m.y)));}
bool operator <=(xy&m){return((unsigned(x)<=unsigned(m.x))&&(unsigned(y)<=unsigned(m.y)));}
bool operator >=(xy&m){return((unsigned(x)>=unsigned(m.x))&&(unsigned(y)>=unsigned(m.y)));}
bool operator ==(xy&m){return((unsigned(x)==unsigned(m.x))&&(unsigned(y)==unsigned(m.y)));}
bool operator  <(int m){return((unsigned(x) <unsigned(m))&&(unsigned(y) <unsigned(m)));}
bool operator  >(int m){return((unsigned(x) >unsigned(m))&&(unsigned(y) >unsigned(m)));}
bool operator <=(int m){return((unsigned(x)<=unsigned(m))&&(unsigned(y)<=unsigned(m)));}
bool operator >=(int m){return((unsigned(x)>=unsigned(m))&&(unsigned(y)>=unsigned(m)));}
bool operator ==(int m){return((unsigned(x)==unsigned(m))&&(unsigned(y)==unsigned(m)));}
xy operator  !(){return xy( !x, !y);}
xy operator  ~(){return xy( ~x, ~y);}
xy operator  +(){return xy( +x, +y);}
xy operator  -(){return xy( -x, -y);}

xy get_number(){
xy m=0;
if(x>0)m.x=+x;if(x<0)m.x=-x;
if(y>0)m.y=+y;if(y<0)m.y=-y;
return m;
}
xy get_flag(){
xy m=0;
if(x>0)m.x=+1;if(x<0)m.x=-1;
if(y>0)m.y=+1;if(y<0)m.y=-1;
return m;
}

int sqrt_xx_yy(){
return sqrt(xx_yy());
}
void x_y(){
int v=x;x=y;y=v;
}
int xx(){return x*x;}
int yy(){return y*y;}
int xx_yy(){
return (x*x+y*y);
}

xy operator =(xy&m){
x=m.x;
y=m.y;
return *this;
}
xy operator =(int m){
x=m;
y=m;
return *this;
}
xy operator ++(int){
xy temp=*this;
++x;
++y;
return temp;
}
xy operator ++(){
x++;
y++;
return *this;
}

xy operator --(int){
xy temp=*this;
--x;
--y;
return temp;
}
xy operator --(){
x--;
y--;
return *this;
}

};

struct xyf{
float x;
float y;

xyf(){
}
xyf(float m){
x=m;
y=m;
}
xyf(float x,float y){
this->x=x;
this->y=y;
}
xyf(xy m){
this->x=m.x;
this->y=m.y;
}
xyf operator  *(xyf&m){return xyf(x *m.x,y *m.y);}
xyf operator  /(xyf&m){return xyf(x /m.x,y /m.y);}
xyf operator  +(xyf&m){return xyf(x +m.x,y +m.y);}
xyf operator  -(xyf&m){return xyf(x -m.x,y -m.y);}
xyf operator  *=(xyf&m){x *=m.x;y *=m.y;return *this;}
xyf operator  /=(xyf&m){x /=m.x;y /=m.y;return *this;}
xyf operator  +=(xyf&m){x +=m.x;y +=m.y;return *this;}
xyf operator  -=(xyf&m){x -=m.x;y -=m.y;return *this;}
xyf operator  *(float m){return xyf(x *m,y *m);}
xyf operator  /(float m){return xyf(x /m,y /m);}
xyf operator  +(float m){return xyf(x +m,y +m);}
xyf operator  -(float m){return xyf(x -m,y -m);}
xyf operator  *=(float m){x *=m;y *=m;return *this;}
xyf operator  /=(float m){x /=m;y /=m;return *this;}
xyf operator  +=(float m){x +=m;y +=m;return *this;}
xyf operator  -=(float m){x -=m;y -=m;return *this;}

xyf get_number(){
xyf m=0;
if(x>0)m.x=+x;if(x<0)m.x=-x;
if(y>0)m.y=+y;if(y<0)m.y=-y;
return m;
}
xyf get_flag(){
xyf m=0;
if(x>0)m.x=+1;if(x<0)m.x=-1;
if(y>0)m.y=+1;if(y<0)m.y=-1;
return m;
}

float sqrt_xx_yy(){
return sqrt(xx_yy());
}
void x_y(){
float v=x;x=y;y=v;
}
float xx(){return x*x;}
float yy(){return y*y;}
float xx_yy(){
return (x*x+y*y);
}

xyf operator =(xy&m){
x=m.x;
y=m.y;
return *this;
}
xyf operator =(xyf&m){
x=m.x;
y=m.y;
return *this;
}
xyf operator =(float m){
x=m;
y=m;
return *this;
}
xyf operator ++(int){
xyf temp=*this;
++x;
++y;
return temp;
}
xyf operator ++(){
x++;
y++;
return *this;
}

xyf operator --(int){
xyf temp=*this;
--x;
--y;
return temp;
}
xyf operator --(){
x--;
y--;
return *this;
}

};

int paint_line(xy r,xy *da){
// float->int (f+0.5)
// [-0.5,+0.5)=0
// [+0.5,+1.5)=1
// [+1.5,+2.5)=2
// x*a+b*y=0;
// (x+v)*a=x*a+v*a;
// (y+v)*a=y*a+v*a;
xy n=r.get_number();
xy f=r.get_flag();
xy nn=n*2;
xy d=0;
xy*db=da;
*db++=d;
if(n.y<n.x){
int v=n.x;
while(d.x!=r.x){
d.x+=f.x;
v-=nn.y;
if(v<0){
v+=nn.x;
d.y+=f.y;
}
*db++=d;
}
}else{
int v=n.y;
while(d.y!=r.y){
d.y+=f.y;
v-=nn.x;
if(v<0){
v+=nn.y;
d.x+=f.x;
}
*db++=d;
}
}
return (db-da);
}
int paint_line(xy a,xy b,xy *da){
int dn=paint_line(b-a,da);
for(int i=0;i<dn;i++)da[i]+=a;
return dn;
}

/*
struct xy64{
__int64 x;
__int64 y;

xy64(){
}
xy64(__int64 m){
x=m;
y=m;
}
xy64(__int64 x,__int64 y){
this->x=x;
this->y=y;
}

xy64 operator  *(xy64&m){return xy64(x *m.x,y *m.y);}
xy64 operator  /(xy64&m){return xy64(x /m.x,y /m.y);}
xy64 operator  %(xy64&m){return xy64(x %m.x,y %m.y);}
xy64 operator  +(xy64&m){return xy64(x +m.x,y +m.y);}
xy64 operator  -(xy64&m){return xy64(x -m.x,y -m.y);}
xy64 operator <<(xy64&m){return xy64(x<<m.x,y<<m.y);}
xy64 operator >>(xy64&m){return xy64(x>>m.x,y>>m.y);}
xy64 operator  &(xy64&m){return xy64(x &m.x,y &m.y);}
xy64 operator  |(xy64&m){return xy64(x |m.x,y |m.y);}
xy64 operator  ^(xy64&m){return xy64(x ^m.x,y ^m.y);}
xy64 operator  *=(xy64&m){x *=m.x;y *=m.y;return *this;}
xy64 operator  /=(xy64&m){x /=m.x;y /=m.y;return *this;}
xy64 operator  %=(xy64&m){x %=m.x;y %=m.y;return *this;}
xy64 operator  +=(xy64&m){x +=m.x;y +=m.y;return *this;}
xy64 operator  -=(xy64&m){x -=m.x;y -=m.y;return *this;}
xy64 operator <<=(xy64&m){x<<=m.x;y<<=m.y;return *this;}
xy64 operator >>=(xy64&m){x>>=m.x;y>>=m.y;return *this;}
xy64 operator  &=(xy64&m){x &=m.x;y &=m.y;return *this;}
xy64 operator  |=(xy64&m){x |=m.x;y |=m.y;return *this;}
xy64 operator  ^=(xy64&m){x ^=m.x;y ^=m.y;return *this;}
xy64 operator  *(__int64 m){return xy64(x *m,y *m);}
xy64 operator  /(__int64 m){return xy64(x /m,y /m);}
xy64 operator  %(__int64 m){return xy64(x %m,y %m);}
xy64 operator  +(__int64 m){return xy64(x +m,y +m);}
xy64 operator  -(__int64 m){return xy64(x -m,y -m);}
xy64 operator <<(__int64 m){return xy64(x<<m,y<<m);}
xy64 operator >>(__int64 m){return xy64(x>>m,y>>m);}
xy64 operator  &(__int64 m){return xy64(x &m,y &m);}
xy64 operator  |(__int64 m){return xy64(x |m,y |m);}
xy64 operator  ^(__int64 m){return xy64(x ^m,y ^m);}
xy64 operator  *=(__int64 m){x *=m;y *=m;return *this;}
xy64 operator  /=(__int64 m){x /=m;y /=m;return *this;}
xy64 operator  %=(__int64 m){x %=m;y %=m;return *this;}
xy64 operator  +=(__int64 m){x +=m;y +=m;return *this;}
xy64 operator  -=(__int64 m){x -=m;y -=m;return *this;}
xy64 operator <<=(__int64 m){x<<=m;y<<=m;return *this;}
xy64 operator >>=(__int64 m){x>>=m;y>>=m;return *this;}
xy64 operator  &=(__int64 m){x &=m;y &=m;return *this;}
xy64 operator  |=(__int64 m){x |=m;y |=m;return *this;}
xy64 operator  ^=(__int64 m){x ^=m;y ^=m;return *this;}
bool operator  <(xy64&m){return((unsigned(x) <unsigned(m.x))&&(unsigned(y) <unsigned(m.y)));}
bool operator  >(xy64&m){return((unsigned(x) >unsigned(m.x))&&(unsigned(y) >unsigned(m.y)));}
bool operator <=(xy64&m){return((unsigned(x)<=unsigned(m.x))&&(unsigned(y)<=unsigned(m.y)));}
bool operator >=(xy64&m){return((unsigned(x)>=unsigned(m.x))&&(unsigned(y)>=unsigned(m.y)));}
bool operator ==(xy64&m){return((unsigned(x)==unsigned(m.x))&&(unsigned(y)==unsigned(m.y)));}
bool operator  <(__int64 m){return((unsigned(x) <unsigned(m))&&(unsigned(y) <unsigned(m)));}
bool operator  >(__int64 m){return((unsigned(x) >unsigned(m))&&(unsigned(y) >unsigned(m)));}
bool operator <=(__int64 m){return((unsigned(x)<=unsigned(m))&&(unsigned(y)<=unsigned(m)));}
bool operator >=(__int64 m){return((unsigned(x)>=unsigned(m))&&(unsigned(y)>=unsigned(m)));}
bool operator ==(__int64 m){return((unsigned(x)==unsigned(m))&&(unsigned(y)==unsigned(m)));}
xy64 operator  !(){return xy64( !x, !y);}
xy64 operator  ~(){return xy64( ~x, ~y);}
xy64 operator  +(){return xy64( +x, +y);}
xy64 operator  -(){return xy64( -x, -y);}

xy64 get_number(){
xy64 m=0;
if(x>0)m.x=+x;if(x<0)m.x=-x;
if(y>0)m.y=+y;if(y<0)m.y=-y;
return m;
}
xy64 get_flag(){
xy64 m=0;
if(x>0)m.x=+1;if(x<0)m.x=-1;
if(y>0)m.y=+1;if(y<0)m.y=-1;
return m;
}

__int64 sqrt_xx_yy(){
return sqrt(xx_yy());
}
void x_y(){
__int64 v=x;x=y;y=v;
}
__int64 xx(){return x*x;}
__int64 yy(){return y*y;}
__int64 xx_yy(){
return (x*x+y*y);
}

xy64 operator =(xy64&m){
x=m.x;
y=m.y;
return *this;
}
xy64 operator =(__int64 m){
x=m;
y=m;
return *this;
}
xy64 operator ++(int){
xy64 temp=*this;
++x;
++y;
return temp;
}
xy64 operator ++(){
x++;
y++;
return *this;
}

xy64 operator --(int){
xy64 temp=*this;
--x;
--y;
return temp;
}
xy64 operator --(){
x--;
y--;
return *this;
}

};
void bezier_call(xy64*sa,int sn,__int64 ti,int bit){
if(sn<=1)return;
for(int si=0;(si+1)<sn;si++)sa[si]=sa[si]+(((sa[si+1]-sa[si])*ti)>>bit);
bezier_call(sa,sn-1,ti,bit);
}
int bezier(xy*sa,int sn,xy*da){
xy64 ta[4];
if(1<=sn&&sn<=nof(ta)){}else return 0;
xy tnxy=0;
for(int si=0;(si+1)<sn;si++){
tnxy+=(sa[si+1]-sa[si]).get_number();// abs()
}
int tn=tnxy.x+tnxy.y;
if(tn>0){}else return 0;

int bit;
for(bit=0;(1<<bit)<tn;bit++);
tn=1<<bit;/
if(bit<=16){}else return 0;
const int bit3=bit*3;

xy64 sa64[nof(ta)];
for(int i=0;i<sn;i++){
sa64[i]=xy64(sa[i].x,sa[i].y);
sa64[i]<<=bit3;
};

int dn=0;
for(int ti=0;ti<tn;ti++){

for(int i=0;i<sn;i++)ta[i]=sa64[i];

bezier_call(ta,sn,ti,bit);
xy t(ta[0].x>>bit3,ta[0].y>>bit3);

for(int di=0;1;di++){
if(di<dn){
if(t==da[di])break;
}else{
da[dn++]=t;
break;
}
}

}
return dn;
}

*/

void bezier_call(xyf*sa,int sn,float ti_div_tn){
if(sn<=1)return;
for(int si=0;(si+1)<sn;si++)sa[si]=sa[si]+(sa[si+1]-sa[si])*ti_div_tn;
bezier_call(sa,sn-1,ti_div_tn);
}
int bezier(xy*sa,int sn,xy*da){
xyf ta[100];
if(1<=sn&&sn<=nof(ta)){}else return 0;
xy tnxy=0;
for(int si=0;(si+1)<sn;si++){
tnxy+=(sa[si+1]-sa[si]).get_number();// abs()
}
const int tn=tnxy.x+tnxy.y;
if(tn==0)return 0;
if(sn==2){
return paint_line(sa[0],sa[1],da);
}
if(sn==3){
xyf a=sa[0];
xyf b=sa[1];
xyf c=sa[2];
int dn=0;
for(int ti=0;ti<tn;ti++){
xyf tf=a+(b-a)*ti/float(tn);
xy t(tf.x,tf.y);
for(int di=0;1;di++){
if(di<dn){
if(t==da[di])break;
}else{
da[dn++]=t;
break;
}
}
}
return dn;
}

if(sn==4){
xyf a=sa[0];
xyf b=sa[1];
xyf c=sa[2];
xyf d=sa[3];

int dn=0;
for(int ti=0;ti<tn;ti++){
float tt=ti/float(tn);
xyf ta=a+(b-a)*tt;
xyf tb=b+(c-b)*tt;
xyf tf=ta+(tb-ta)*tt;
xy t(tf.x,tf.y);
for(int di=0;1;di++){
if(di<dn){
if(t==da[di])break;
}else{
da[dn++]=t;
break;
}
}

}
return dn;
}

int dn=0;
for(int ti=0;ti<tn;ti++){

for(int i=0;i<sn;i++)ta[i]=sa[i];

bezier_call(ta,sn,ti/float(tn));
xy t(ta[0].x,ta[0].y);
for(int di=0;1;di++){
if(di<dn){
if(t==da[di])break;
}else{
da[dn++]=t;
break;
}
}

}
return dn;
}

const int g=1;
void sets(void (*set)(int color,xy*da,int dn,int*wa,xy w),int*wa,int width,int height,xy pos){
xy w(width,height);
//for(int n=w.x*w.y,i=0;i<n;i++)wa[i]=0;
xy wn=w/g;
xy wo=wn/2;
xy w1=xy(wn.x/4,wn.y/2);
xy w2=xy(wn.x*3/4,wn.y/2);
xy wr=pos/g;
if(wr<wn){
xy  da[xymax*xymax];
int dn;

//dn=paint_line(wo,wr,da);set(0x008000,da,dn,wa,w);
//dn=paint_circle(wo,(wr-wo).sqrt_xx_yy(),da),set(0x008000,da,dn,wa,w);
//dn=paint_ellipse(wo,wr,da);set(0x008000,da,dn,wa,w);

// dn=fill_line(wo,wr,da,20);set(0x008000,da,dn,wa,w);
dn=fill_circle(wo,(wr-wo).sqrt_xx_yy(),da),set(0x008000,da,dn,wa,w);
// dn=fill_ellipse(wo,wr,da);set(0x008000,da,dn,wa,w);

xy sa[]={wo/2,wo,wr};

for(int si=0;(si+1)<=nof(sa);si++){dn=paint_line(sa[si%nof(sa)],sa[(si+1)%nof(sa)],da);set(0x808080,da,dn,wa,w); }
dn=bezier(sa,nof(sa),da);set(0x008000,da,dn,wa,w);

}

}
// 妄想扣篮的男人 贝塞尔曲线 知识来源：
// 1,九年义务教育
// 2,百度百科 贝塞尔曲线
// 3,微软文档 PolyBezier
struct get_bezier{
void get(char(*sa)[1000],int sn){
if(sn<=1)return;
for(int si=0;(si+1)<sn;si++){
char a[1000];sprintf(a,"%s",sa[si]);
sprintf(sa[si],"(%s+(%s-%s)*i_div_n)",a,sa[si+1],a);
}
get(sa,sn-1);
}
get_bezier(){
char da[0x1000];
const int sn=4;
char sa[sn][1000];
for(int si=0;si<sn;si++)sprintf(sa[si],"%c",'a'+si);
get(sa,sn);
printf("%s\n",*sa);
// (a+(b-a)*i_div_n)
// ((a+(b-a)*i_div_n)+((b+(c-b)*i_div_n)-(a+(b-a)*i_div_n))*i_div_n)
// (((a+(b-a)*i_div_n)+((b+(c-b)*i_div_n)-(a+(b-a)*i_div_n))*i_div_n)+(((b+(c-b)*i_div_n)+((c+(d-c)*i_div_n)-(b+(c-b)*i_div_n))*i_div_n)-((a+(b-a)*i_div_n)+((b+(c-b)*i_div_n)-(a+(b-a)*i_div_n))*i_div_n))*i_div_n)
exit(0);
}
}
//get_bezier
;



08-21 944

04-28 8796
09-01 3727
02-17 428
04-25 289
02-20 2214
08-02 874
10-17 346
04-05 872
03-23 5596
12-26 47
01-05 311
06-25 1万+
12-29 4707
06-24 49

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。