Beispiel #1
0
void drawCubicBezier(HDC hdc,ALPoint b, ALPoint e, ALPoint c1, ALPoint c2) {
	GdiPlusIniter ginit;
	Graphics *graphics = Graphics::FromHDC(hdc);
	graphics->SetSmoothingMode(SmoothingModeHighQuality);
	Point p1(b.x, -b.y);
	Point p2(c1.x, -c1.y);
	Point p3(c2.x, -c2.y);
	Point p4(e.x, -e.y);
	Pen pen(Color::Red);
	pen.SetLineJoin(LineJoinRound);
	pen.SetStartCap(LineCapRound);
	pen.SetEndCap(LineCapRound);
	graphics->DrawBezier(&pen, p1,p2,p3,p4); 
	/*
	AlPointsArray points(50,5);
	float t = 0;
	while(t <= 1) {
		ALPoint	alp = calcuBezierPoint(b,e,c1,c2,t);
		alp.y = (negative?-1:1)*alp.y;
		points.append(alp);
		t += 0.02;
	}
	drawPoly(hdc,points);
	*/
}
Beispiel #2
0
// #define DrawPolygonMacro
void drawBezierSmoothPoly(HDC hdc, AlpointsList &points, float f, COLORREF color) {
	if(points.count()<3) return;
	GdiPlusIniter ginit;
	Graphics *graphics = Graphics::FromHDC(hdc);
	graphics->SetSmoothingMode(SmoothingModeHighQuality);
	graphics->SetCompositingMode(CompositingModeSourceOver);
	Pen pen(Color::Red);
	pen.SetLineJoin(LineJoinRound);
	pen.SetLineCap(LineCapRound,LineCapRound, DashCapRound);
// 绘制多边形
#if defined(DrawPolygonMacro)
	pen.SetColor(Color::Blue);
	pen.SetDashStyle(DashStyleDash);
	for(int i=0; i<points.count() - 1; i++){
		Point p1(points[i+0].x, -points[i+0].y);
		Point p2(points[i+1].x, -points[i+1].y);
		graphics->DrawLine(&pen, p1, p2 );
	}
#endif
#if defined(ThroghtEveryPoint)
	ALPoint b, e, n, c1, c2, c3;
	int last = 0;
	for(int i=0; i<points.count()-1; i++){
		if( distance(points[last], points[i+1])<4 ){
			continue;
		}
		b = points[last];
		e = points[i+1];
		n = points[i+2];
		c1 = last==0? points[0]:c3;
		c2 = cubicBezierControlPoint(b, e, n, &c3, f);
		{
			// 绘制控制点!!!
#if 0
			pen.SetDashStyle(DashStyleDot);
			pen.SetColor(Color::Green);
			Point p1(c2.x, -c2.y);
			Point p2(c3.x, -c3.y);
			graphics->DrawEllipse(&pen, p1.X-1, p1.Y-1,2,2);
			graphics->DrawEllipse(&pen, p2.X-1, p2.Y-1,2,2);
			graphics->DrawLine(&pen, p1, p2);
#endif
		}
		pen.SetDashStyle(DashStyleSolid);
		pen.SetColor(Color::Red);
		Point p1(b.x, -b.y);
		Point p2(c1.x, -c1.y);
		Point p3(c2.x, -c2.y);
		Point p4(e.x, -e.y);
		graphics->DrawBezier(&pen, p1,p2,p3,p4); 
		last = i+1;
	} 
#else  // 使用顶点作为控制点!!!!!
	pen.SetColor(Color::Red);
	pen.SetDashStyle(DashStyleSolid);
	int last = 0;
	float bx,by,cx,cy,ex,ey;
	for(int i=0; i<points.count(); i++){
		bx = i==0?points[0].x:ex;
		by = i==0?points[0].y:ey;
		ex = (points[i+1].x + points[i+2].x)/2;
		ey = (points[i+1].y + points[i+2].y)/2;
		cx = points[i+1].x; cy = points[i+1].y;
		// b(t) = (1-t)^2*p0 + 2t(1-t)*p1 + t^2*p2
		float x0 = bx, y0 = -by;
		for(float t=0.02; t<=1.0; t+=0.02){
			float x1 = square(1-t)*bx + 2*t*(1-t)*cx + square(t)*ex;
			float y1 = square(1-t)*by + 2*t*(1-t)*cy + square(t)*ey;
			y1 = -y1;
			graphics->DrawLine(&pen,x0, y0,x1,y1);
			x0 = x1; y0 = y1;
		}
	}

#endif
}