/************************
*filename:snowcurve.cpp*
*Author:luosunny126 *
*15:28 2008-3-29 *
************************/
#include"snowcurve.h"
#include<malloc.h>
#include<stdio.h>
#include<math.h>
#include<windows.h>
// 获取一个可供画图的DC,我这里就直接用桌面算了
HDC hdc = GetWindowDC( GetDesktopWindow() );
// 创建红色1像素宽度的实线画笔
HPEN hpen1 = CreatePen( PS_SOLID, 1, RGB(255,0,0) );
// 将hpen1和hbrush1选进HDC,并保存HDC原来的画笔
HPEN hpen_old = (HPEN)SelectObject( hdc, hpen1 );
SCurve *head;
int drawSnowCurve(int n,int side,SNOW center)
{
SCurve *Q1,*Q2;
if(n>SnowCN || n<1) return ERROR;
InitSnowCurve(side,center);
getSnowCurve( n );
Q1=head->next;
Q2=Q1->next;
while(Q2!=NULL)
{
drawLine( Q1->data,Q2->data );
Q1=Q2;
Q2=Q2->next;
}
freeSCoords();//释放空间
return OK;
}
void getSnowCurve(int n)
{ int m=1;
SCurve *p1,*p2;
while(m<=n)
{
p1=head->next;
p2=p1->next;
while(p2!=NULL)
{
getThreeCoords( p1 );
p1=p2;
p2=p2->next;
}
m++;
}
}
int getThreeCoords(SCurve *p)
{
SNOW cd1,cd2,cd3,cd4,cd5;
cd1=p->data;
cd2=p->next->data;
cd3.x=cd1.x + (cd2.x-cd1.x)/3 ;
cd3.y=cd1.y + (cd2.y-cd1.y)/3 ;
cd4.x=cd1.x + 2*(cd2.x-cd1.x)/3 ;
cd4.y=cd1.y + 2*(cd2.y-cd1.y)/3 ;
int flag=(cd2.y-cd1.y) / (cd2.x-cd1.x);
if(flag == 0)
{
cd5.x=(cd1.x+cd2.x)/2 ;
cd5.y=(int)( sqrt(3.0)*(cd4.x-cd3.x)/2 ) +cd3.y;
}
else if(flag > 0)
{
cd5.x=2*cd3.x - cd4.x;
cd5.y=cd4.y;
}
else
{ cd5.x=2*cd4.x - cd3.x;
cd5.y=cd3.y;
}
if(insertSCoord(p,cd3))
if(insertSCoord(p->next,cd5))
if(insertSCoord(p->next->next,cd4))
return OK;
return ERROR ;
}
int insertSCoord(SCurve *p,SNOW cd)
{ SCurve *p1;
p1=(SCurve *)malloc(sizeof(SCurve));
if(p1==NULL) return OVERFLOW;
p1->data=cd;
p1->next=p->next;
p->next=p1;
return OK;
}
int InitSnowCurve(int side,SNOW center)
{ int i;
SCurve *pi;
SNOW coord[4];
head=(SCurve *)malloc(sizeof(SCurve));
if(head==NULL) return OVERFLOW;
head->next=NULL;
pi=head;
coord[0].x=center.x - side/2 ;
coord[0].y=center.y + (int)( sqrt(3.0)*side/6 );
coord[1].x=center.x +side/2 ;
coord[1].y=coord[0].y;
coord[2].x=center.x ;
coord[2].y=coord[0].y - (int)( sqrt(3.0)*side/2 );
coord[3] = coord[0];
for(i=0;i<4;i++)
{
insertSCoord( pi, coord[i]);
pi=pi->next;
}
return OK;
}
void drawLine(SNOW cd1,SNOW cd2)
{
MoveToEx( hdc, cd1.x, cd1.y, NULL );
LineTo( hdc, cd2.x, cd2.y );
}
void freeSCoords()
{
SCurve *q, *p=head;
while(p!=NULL)
{ q=p;
p=p->next;
free(q);q=NULL;
}
}
luosunny126
- 粉丝: 0
- 资源: 1