樹心幽徑

20170710Snowflake with Triangle initiator
2017/07/10,06:14

http://oldwww.kmvs.km.edu.tw/lf/gallery/70/TriSnowFlake0710.JPG

alt

//g++  `Magick++-config --cxxflags --cppflags` -I/usr/local/include/ImageMagick-7  segb.c `Magick++-config --ldflags --libs`  -L/usr/local/zlib/lib -lz

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <Magick++.h>
using namespace Magick;
void drawSide(int L,int ox,int oy, int fx ,int fy,double rad,Image *pim){
//O--M--W--N--F
int mx,my,nx,ny,wx,wy;
pim->strokeColor("red");
int w=pow((fy-oy)*(fy-oy)+(fx-ox)*(fx-ox),0.5);
int dx=w*cos(rad);
int dy=-w*sin(rad);
mx=ox+dx/3;
my=oy+dy/3;
pim->draw(DrawableLine(ox,oy,mx,my));
int w2=w/3;
int dx2=w2*cos(rad+M_PI/3);
int dy2=-w2*sin(rad+M_PI/3);
wx=mx+dx2;
wy=my+dy2;
pim->strokeColor("green");
pim->draw(DrawableLine(mx,my,wx,wy));
nx=ox+2*dx/3;
ny=oy+2*dy/3;
pim->draw(DrawableLine(wx,wy,nx,ny));
pim->strokeColor("blue");
pim->draw(DrawableLine(nx,ny,fx,fy));
pim->strokeColor("white");
pim->draw(DrawableArc((mx+nx+wx)/3-w2/4,(my+ny+wy)/3-w2/4,(mx+nx+wx)/3+w2/4,(my+ny+wy)/3+w2/4,0,360));

if (L>0) {
    drawSide(L-1,ox,oy,mx,my,rad,pim);
    drawSide(L-1,mx,my,wx,wy,rad+M_PI/3,pim);
    drawSide(L-1,wx,wy,nx,ny,rad-M_PI/3,pim);
    drawSide(L-1,nx,ny,fx,fy,rad,pim);
}
}
void drawTriLine(int L,int ox,int oy, int w, int h,Image *pim){
int gx,gy,fx,fy;
pim->strokeColor("red");
fx=ox+w*cos(M_PI/3);
fy=oy-w*sin(M_PI/3);
gx=ox+w;
gy=oy;
printf("O(%d,%d) w=%d,h=%d\n",ox,oy,w,h);
printf("1:(%d,%d)-(%d,%d)\n",ox,oy,fx,fy);
pim->draw(DrawableLine(ox,oy,fx,fy));
printf("2:(%d,%d)-(%d,%d)\n",fx,fy,gx,gy);
pim->draw(DrawableLine(fx,fy,gx,gy));
printf("3:(%d,%d)-(%d,%d)\n",gx,gy,ox,oy);
pim->draw(DrawableLine(gx,gy,ox,oy));
 
drawSide(4,ox,oy,fx,fy,M_PI/3,pim);
drawSide(4,fx,fy,gx,gy,-M_PI/3,pim);
drawSide(4,gx,gy,ox,oy,-M_PI,pim);
pim->draw(DrawableArc((ox+fx+gx)/3-w/4,(oy+fy+gy)/3-w/4,(ox+fx+gx)/3+w/4,(oy+fy+gy)/3+w/4,0,360));

}

void drawTri(int L,int ox,int oy, int w, int h,Image *pim){
int ax,ay;
pim->strokeColor("white");
pim->draw(DrawableLine(ox+0,oy+h-1,ox+w,oy+h-1));
pim->draw(DrawableLine(ox+0,oy+h-1,ox+w*sin(M_PI/6),oy+h-1-w*cos(M_PI/6)));
pim->draw(DrawableLine(ox+w,oy+h-1,ox+w*sin(M_PI/6),oy+h-1-w*cos(M_PI/6)));
if(L>0){
    ax=ox+3*w/6; ay=oy+h-1-w*cos(M_PI/6)/3-w/3;
    drawTri(L-1,ax,ay,w/3,w/3,pim);
    ax=ox+w/6; ay=oy+h-1-w*cos(M_PI/6)*1/3-w/3;
    drawTri(L-1,ax,ay,w/3,w/3,pim);
    ax=ox+w/3; ay=oy+h-1-w*cos(M_PI/6)*2/3-w/3;
    drawTri(L-1,ax,ay,w/3,w/3,pim);
    ax=ox+0*w/3; ay=oy+h-1-w/3;
    drawTri(L-1,ax,ay,w/3,w/3,pim);
    ax=ox+1*w/3; ay=oy+h-1-w/3;
    drawTri(L-1,ax,ay,w/3,w/3,pim);
    ax=ox+2*w/3; ay=oy+h-1-w/3;
    drawTri(L-1,ax,ay,w/3,w/3,pim);
}
}

int main()
{
int w=3*7*5*10;
int h=3*7*5*14;
Image im( Geometry(w, h), Color("black"));
im.draw(DrawableStrokeColor(Color("black")));
im.strokeWidth(2);
drawTriLine(3,0,w,w,w,&im);
im.write("TriSnowFlake-1.jpg");
}

20170708Koch-snowflake(three segment von koch curve)Last
2017/07/08,23:07

alt

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <Magick++.h>
using namespace Magick;
void drawTen(int L,int ox,int oy, int w, int h,Image *pim){
if(L>0){
int ax,ay,bx,by,cx,cy,dx,dy,ex,ey;
int k=(L-1)/2;
int p5k=pow(5,k);
int wn,hn,wp,hp;
    if(L%2==1){
    wn=w/(7*p5k); wp=3*p5k*wn;
    hn=h/(7*p5k); hp=3*p5k*hn;
    ax=ox+2*p5k*wn; ay=oy+2*p5k*hn;
    bx=ox+1*p5k*wn; by=oy+0*p5k*hn;
    cx=ox+4*p5k*wn; cy=oy+1*p5k*hn;
    dx=ox+3*p5k*wn; dy=oy+4*p5k*hn;
    ex=ox+0*p5k*wn; ey=oy+3*p5k*hn;
    }
    else{
    wn=w/(15*p5k); wp=7*p5k*wn;
    hn=h/(15*p5k); hp=7*p5k*hn;
    ax=ox+4*p5k*wn; ay=oy+4*p5k*hn;
    bx=ox+0*p5k*wn; by=oy+1*p5k*hn;
    cx=ox+7*p5k*wn; cy=oy+0*p5k*hn;
    dx=ox+8*p5k*wn; dy=oy+7*p5k*hn;
    ex=ox+1*p5k*wn; ey=oy+8*p5k*hn;
    }
    drawTen(L-1,ax,ay,wp,hp,pim);
    drawTen(L-1,bx,by,wp,hp,pim);
    drawTen(L-1,cx,cy,wp,hp,pim);
    drawTen(L-1,dx,dy,wp,hp,pim);
    drawTen(L-1,ex,ey,wp,hp,pim);
}
if(L==0){
pim->strokeColor("white");
pim->draw(DrawableLine(ox+w/3,oy+0,ox+w/3,oy+h));
pim->draw(DrawableLine(ox+2*w/3,oy+0,ox+2*w/3,oy+h));
pim->draw(DrawableLine(ox+0,oy+h/3,ox+w,oy+h/3));
pim->draw(DrawableLine(ox+0,oy+2*h/3,ox+w,oy+2*h/3));
pim->draw(DrawableLine(ox,oy+h/3,ox,oy+2*h/3));
pim->draw(DrawableLine(ox+w-1,oy+h/3,ox+w-1,oy+2*h/3));
pim->draw(DrawableLine(ox+w/3,oy,ox+2*w/3,oy));
pim->draw(DrawableLine(ox+w/3,oy+h-1,ox+2*w/3,oy+h-1));
pim->strokeColor("red");
pim->draw(DrawableArc(ox+w/3+2,oy+h/3+2,ox+w*2/3-2,oy+h*2/3-2,0,360));
}
}

int main()
{
int w=3*7*5*10;
int h=3*7*5*10;
Image im( Geometry(w, h), Color("black"));
im.draw(DrawableStrokeColor(Color("black")));
im.strokeWidth(2);
drawTen(5,0,0,w,h,&im);
im.write("seg0708-5.jpg");
drawTen(4,0,0,w,h,&im);
im.write("seg0708-4.jpg");
drawTen(3,0,0,w,h,&im);
im.write("seg0708-3.jpg");
drawTen(2,0,0,w,h,&im);
im.write("seg0708-2.jpg");
drawTen(1,0,0,w,h,&im);
im.write("seg0708-1.jpg");
}

 

20170708Koch-snowflake(three segment von koch curve)
2017/07/08,12:40

alt

alt

altalt

//g++  `Magick++-config --cxxflags --cppflags` -I/usr/local/include/ImageMagick-7  segb.c `Magick++-config --ldflags --libs`  -L/usr/local/zlib/lib -lz

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <Magick++.h>
using namespace Magick;
void drawTen(int L,int ox,int oy, int w, int h,Image *pim){
if (L==1){
    drawTen(L-1,ox+2*w/7,oy+2*h/7,3*w/7,3*h/7,pim);
    drawTen(L-1,ox+1*w/7,oy+0*h/7,3*w/7,3*h/7,pim);
    drawTen(L-1,ox+4*w/7,oy+1*h/7,3*w/7,3*h/7,pim);
    drawTen(L-1,ox+3*w/7,oy+4*h/7,3*w/7,3*h/7,pim);
    drawTen(L-1,ox+0*w/7,oy+3*h/7,3*w/7,3*h/7,pim);
    }
if (L==2){
    drawTen(L-1,ox+4*w/15,oy+4*h/15,7*w/15,7*h/15,pim);
    drawTen(L-1,ox+0*w/15,oy+1*h/15,7*w/15,7*h/15,pim);
    drawTen(L-1,ox+7*w/15,oy+0*h/15,7*w/15,7*h/15,pim);
    drawTen(L-1,ox+8*w/15,oy+7*h/15,7*w/15,7*h/15,pim);
    drawTen(L-1,ox+1*w/15,oy+8*h/15,7*w/15,7*h/15,pim);
    }
if (L==3){
    drawTen(L-1,ox+10*w/35,oy+10*h/35,15*w/35,15*h/35,pim);
    drawTen(L-1,ox+ 5*w/35,oy+ 0*h/35,15*w/35,15*h/35,pim);
    drawTen(L-1,ox+20*w/35,oy+ 5*h/35,15*w/35,15*h/35,pim);
    drawTen(L-1,ox+15*w/35,oy+20*h/35,15*w/35,15*h/35,pim);
    drawTen(L-1,ox+ 0*w/35,oy+15*h/35,15*w/35,15*h/35,pim);
    }
if (L==4){
    drawTen(L-1,ox+20*w/75,oy+20*h/75,35*w/75,35*h/75,pim);
    drawTen(L-1,ox+ 0*w/75,oy+ 5*h/75,35*w/75,35*h/75,pim);
    drawTen(L-1,ox+35*w/75,oy+ 0*h/75,35*w/75,35*h/75,pim);
    drawTen(L-1,ox+40*w/75,oy+35*h/75,35*w/75,35*h/75,pim);
    drawTen(L-1,ox+ 5*w/75,oy+40*h/75,35*w/75,35*h/75,pim);
    }
if(L==0){
pim->strokeColor("white");
pim->draw(DrawableLine(ox+w/3,oy+0,ox+w/3,oy+h));
pim->draw(DrawableLine(ox+2*w/3,oy+0,ox+2*w/3,oy+h));
pim->draw(DrawableLine(ox+0,oy+h/3,ox+w,oy+h/3));
pim->draw(DrawableLine(ox+0,oy+2*h/3,ox+w,oy+2*h/3));
pim->draw(DrawableLine(ox,oy+h/3,ox,oy+2*h/3));
pim->draw(DrawableLine(ox+w-1,oy+h/3,ox+w-1,oy+2*h/3));
pim->draw(DrawableLine(ox+w/3,oy,ox+2*w/3,oy));
pim->draw(DrawableLine(ox+w/3,oy+h-1,ox+2*w/3,oy+h-1));
pim->strokeColor("red");
pim->draw(DrawableArc(ox+w/3+5,oy+h/3+5,ox+w*2/3-5,oy+h*2/3-5,0,360));
}
}

int main()
{
int w=3*7*5*10;
int h=3*7*5*10;
Image im( Geometry(w, h), Color("black"));
im.draw(DrawableStrokeColor(Color("black")));
im.strokeWidth(2);
drawTen(4,0,0,w,h,&im);
im.write("seg0708-4.jpg");
drawTen(3,0,0,w,h,&im);
im.write("seg0708-3.jpg");
drawTen(2,0,0,w,h,&im);
im.write("seg0708-2.jpg");
drawTen(1,0,0,w,h,&im);
im.write("seg0708-1.jpg");
}

 
Accessible and Valid XHTML 1.0 Strict and CSS Powered by LifeType