《图形学》基本算法实现

Unfair

发布日期: 2021-06-01 10:08:08 浏览量: 143
评分:
star star star star star star star star star star_border
*转载请注明来自write-bug.com

Bresenham画线

  1. //Bresenham画线
  2. void bresLine(int x1, int y1, int x2, int y2)
  3. {
  4. TextOut(g_hDc, x2, y2, L"Bresenham画线",11);
  5. int x = x1, y = y1, dx = x2 - x1, dy = y2 - y1, e = -dx;
  6. while (x <= x2)
  7. {
  8. SetPixel(g_hDc,x, y, RGB(0, 255, 0));
  9. x++;
  10. = e + (2 * dy);
  11. if (e > 0)
  12. {
  13. y++;
  14. = e - (2 * dx);
  15. }
  16. }
  17. }

DDA画线

  1. //DDA画线
  2. void DDALine(int x1, int y1, int x2, int y2)
  3. {
  4. TextOut(g_hDc, x2, y2, L"DDA画线",5);
  5. int dx = x2 - x1, dy = y2 - y1, epsl;
  6. float x = x1, y = y1, xIncre, yIncre;
  7. if (abs(dx) > abs(dy))
  8. epsl = abs(dx);
  9. else
  10. epsl = abs(dy);
  11. xIncre = (float)dx / (float)epsl;
  12. yIncre = (float)dy / (float)epsl;
  13. for (int k = 0; k <= epsl; k++)
  14. {
  15. SetPixel(g_hDc,(int)x + 0.5, (int)y + 0.5, RGB(0, 0, 255));
  16. += xIncre;
  17. += yIncre;
  18. }
  19. }

中点Bresenham画线

  1. //中点Bresenham画线
  2. void midBresLine(int x1, int y1, int x2, int y2)
  3. {
  4. TextOut(g_hDc,x2, y2, L"中点Bresenham画线",13);
  5. int dx, dy, d, upIncre, downIncre, x = x1, y = y1;
  6. if (x1 > x2)
  7. {
  8. = x2;
  9. x2 = x1;
  10. x1 = x;
  11. = y2;
  12. y2 = y1;
  13. y1 = y;
  14. }
  15. = x1;
  16. y = y1;
  17. dx = x2 - x1;
  18. dy = y2 - y1;
  19. = dx - 2 * dy;
  20. upIncre = 2 * dx - 2 * dy;
  21. downIncre = -2 * dy;
  22. while (x <= x2)
  23. {
  24. SetPixel(g_hDc,x, y, RGB(0, 255, 255));
  25. x++;
  26. if (d < 0)
  27. {
  28. y++;
  29. += upIncre;
  30. }
  31. else
  32. += downIncre;
  33. }
  34. }

库函数画线

  1. //库函数画线
  2. void libLine(int x1, int y1, int x2, int y2)
  3. {
  4. TextOut(g_hDc, x2, y2, L"库函数画线", 5);
  5. MoveToEx(g_hDc, x1, y1, NULL);
  6. LineTo(g_hDc, x2, y2);
  7. HPEN hPen = CreatePen(PS_DASH, 10, RGB(255, 0, 0));//sPen[]笔型;粗细;颜色
  8. SelectObject(g_hDc, hPen);
  9. MoveToEx(g_hDc, x1, y1-100, NULL);
  10. LineTo(g_hDc, x2, y2-100);
  11. HPEN hPen2 = CreatePen(PS_DOT, 1, RGB(255, 0, 0));//sPen[]笔型;粗细;颜色
  12. SelectObject(g_hDc, hPen2);
  13. MoveToEx(g_hDc, x1, y1 - 200, NULL);
  14. LineTo(g_hDc, x2, y2 - 200);
  15. }

中点Bresenham画圆

  1. //中点Bresenham画圆
  2. static void point(int x, int y, int x0, int y0)
  3. {
  4. SetPixel(g_hDc, x0 + x, y0 + y, RGB(255, 0, 0));
  5. SetPixel(g_hDc, x0 + y, y0 + x, RGB(255, 0, 0));
  6. SetPixel(g_hDc, x0 - y, y0 + x, RGB(0, 255, 0));
  7. SetPixel(g_hDc, x0 - x, y0 + y, RGB(0, 255, 0));
  8. SetPixel(g_hDc, x0 - x, y0 - y, RGB(0, 0, 255));
  9. SetPixel(g_hDc, x0 - y, y0 - x, RGB(0, 0, 255));
  10. SetPixel(g_hDc, x0 + y, y0 - x, RGB(255, 255, 0));
  11. SetPixel(g_hDc, x0 + x, y0 - y, RGB(255, 255, 0));
  12. }
  13. void midBresCircle(int x0, int y0, int r)
  14. {
  15. TextOut(g_hDc, x0, y0, L"Bresenham画圆",11);
  16. int x = 0, y = r, d = 1 - r;
  17. while (x <= y)
  18. {
  19. point(x, y, x0, y0);
  20. if (d < 0)
  21. += 2 * x + 3;
  22. else
  23. {
  24. += 2 * (x - y) + 5;
  25. y--;
  26. }
  27. x++;
  28. }
  29. }

中点Bresenham椭圆

  1. //中点Bresenham椭圆
  2. static void point2(int x, int y, int x0, int y0)
  3. {
  4. SetPixel(g_hDc, x0 + x, y0 + y, RGB(0, 0, 255));
  5. SetPixel(g_hDc, x0 - x, y0 - y, RGB(0, 0, 255));
  6. SetPixel(g_hDc, x0 - x, y0 + y, RGB(0, 0, 255));
  7. SetPixel(g_hDc, x0 + x, y0 - y, RGB(0, 0, 255));
  8. }
  9. void midBresEllipse(int x0, int y0, int a, int b)
  10. {
  11. TextOut(g_hDc, x0, y0, L"中点Bresenham椭圆",13);
  12. int x = 0, y = b;
  13. float d1 = b*b + a*a*(-b + 0.25);
  14. float d2 = b*b*(x + 0.5)*(x + 0.5) + a*a*(y - 1)*(y - 1) - a*a*b*b;
  15. point2(x, y, x0, y0);
  16. while (b*b*(x + 1) < a*a*(y - 0.5))
  17. {
  18. if (d1 <= 0)
  19. {
  20. d1 += b*b*(3 * x + 3);
  21. x++;
  22. }
  23. else
  24. { d1 += b*b*(2 * x + 3) + a*a*(-2 * y + 2);
  25. x++;
  26. y--;
  27. }
  28. point2(x, y, x0, y0);
  29. }
  30. while (y > 0)
  31. {
  32. if (d2 <= 0)
  33. { d2 += b*b*(2 * x + 2) + a*a*(-2 * y + 3);
  34. x++;
  35. y--;
  36. }
  37. else {
  38. d2 += a*a*(-2 * y + 3);
  39. y--;
  40. }
  41. point2(x, y, x0, y0);
  42. }
  43. }

矩形绘制

  1. //矩形绘制
  2. void glRect(int x0, int y0, int x1, int y1)
  3. {
  4. TextOut(g_hDc,x1, y1, L"库函数画矩形",6);
  5. Rectangle(g_hDc,x0, y0, x1, y1);
  6. }

多边形绘制

  1. //多边形绘制
  2. void polygon(int* arrX, int* arrY, int n)
  3. {
  4. TextOut(g_hDc, arrX[0], arrY[0], L"多边形绘制", 5);
  5. MoveToEx(g_hDc, arrX[0], arrY[0], NULL);
  6. for (int i = 0; i < n; ++i)
  7. LineTo(g_hDc, arrX[i], arrY[i]);
  8. LineTo(g_hDc, arrX[0], arrY[0]);
  9. }

绘制弧线

  1. //绘制弧线
  2. void arc(int* arrX,int* arrY)
  3. {
  4. Arc(g_hDc, arrX[0],arrY[0],arrX[1],arrY[1],
  5. arrX[2],arrY[2],arrX[3],arrY[3]);
  6. }

椭圆的平移变换

  1. //椭圆的平移变换
  2. void moveEllipse(int x0, int y0, int x1, int y1)
  3. {
  4. TextOut(g_hDc,x0, y0, L"圆的平移变换",6);
  5. Ellipse(g_hDc,x0, y0, x1, y1);
  6. TextOut(g_hDc, x0 + 300, y0 + 200, L"平移300,200后", 9);
  7. Ellipse(g_hDc, x0 + 300, y0 + 200, x1 + 300, y1 + 200);
  8. }

椭圆的比例变换

  1. //椭圆的比例变换
  2. void rateEllipse(int x0, int y0, int x1, int y1)
  3. {
  4. int Sx = 2, Sy = 2;
  5. Ellipse(g_hDc, x0, y0, x1, y1);
  6. Ellipse(g_hDc, x0*Sx+300, y0*Sy-300, x1*Sx+300, y1*Sy-300);
  7. TextOut(g_hDc, x0*Sx, y0*Sy, L"圆的比例变换", 6);
  8. TextOut(g_hDc, x0*Sx*Sx + 300, y0*Sy*Sy - 300, L"比例变化Sx,Sy后", 10);
  9. }

错切变换Y方向

  1. //错切变换Y方向
  2. void dislocatRect(int x0, int y0, int x1, int y1)
  3. { int dirY = 1;
  4. Rectangle(g_hDc, x0, y0, x1, y1);
  5. int resultX[4] = { x0 + 300, x0 + 300, x1 + 300, x1 + 300 };
  6. int resultY[4] = { y0, y0 + dirY*x1, y1 + dirY*x1, y1 };
  7. polygon(resultX, resultY, 4);
  8. TextOut(g_hDc, x0, y0, L"矩形的错切变换前", 8);
  9. TextOut(g_hDc, x1 + 300, y1, L"矩形的错切变换后", 8);
  10. }

矩形的对称变换

  1. //矩形的对称变换
  2. void symmRect(int x0, int y0, int x1, int y1)
  3. {
  4. TextOut(g_hDc, x0, y0, L"矩形的对称变换前", 8);
  5. Rectangle(g_hDc, x0, y0, x1, y1);
  6. TextOut(g_hDc, -x0, -y0, L"矩形的对称变换后", 8);
  7. Rectangle(g_hDc, -x0, -y0, -x1, -y1);
  8. }

旋转

  1. //旋转
  2. void turnRect()
  3. {
  4. float st = -30 * 3.14 / 180.0;
  5. int arrX[4] = { -100, 100, 100, -100 };
  6. int arrY[4] = { 100, 100, -100, -100 };
  7. polygon(arrX, arrY, 4);
  8. TextOut(g_hDc, arrX[1], arrY[1], L"旋转前", 3);
  9. for (int i = 0; i < 4; ++i)
  10. {
  11. arrX[i] = arrX[i] * cos(st) - arrY[i] * sin(st);
  12. arrY[i] = arrX[i] * sin(st) + arrY[i] * cos(st);
  13. }
  14. polygon(arrX, arrY, 4);
  15. TextOut(g_hDc, arrX[1], arrY[1], L"旋转后", 3);
  16. }

复合平移

  1. //复合平移
  2. void compMove(int x0, int y0, int x1, int y1)
  3. {
  4. Rectangle(g_hDc, x0, y0, x1, y1);
  5. Rectangle(g_hDc, x0, -y0, x1, -y1);
  6. Rectangle(g_hDc, -x0, -y0, -x1, -y1);
  7. TextOut(g_hDc, x0, y0, L"矩形的平移变换前", 8);
  8. TextOut(g_hDc, x0, -y0, L"矩形的平移变换1", 8);
  9. TextOut(g_hDc, -x0, -y0, L"矩形的平移变换2", 8);
  10. }

复合比例

  1. //复合比例
  2. void compRate(int x0, int y0, int x1, int y1)
  3. {
  4. float Sx = 0.5, Sy = 0.5;
  5. float S2x = 2, S2y = 2;
  6. Rectangle(g_hDc, x0*S2x, y0*S2y, x1*S2x, y1*S2y);
  7. Rectangle(g_hDc, x0, y0, x1, y1);
  8. Rectangle(g_hDc, x0*Sx, y0*Sy, x1*Sx, y1*Sy);
  9. TextOut(g_hDc, x0*S2x, y1*S2y, L"矩形的比例变换2倍", 9);
  10. TextOut(g_hDc, x0, y1, L"原图形", 3);
  11. TextOut(g_hDc, x0*Sx, y1*Sy, L"1/2图形", 5);
  12. }

复合旋转

  1. //复合旋转
  2. void compTurn()
  3. {
  4. float st = -30 * 3.14 / 180.0;
  5. int arrX[4] = { -100, 100, 100, -100 };
  6. int arrY[4] = { 100, 100, -100, -100 };
  7. polygon(arrX, arrY, 4);
  8. TextOut(g_hDc, arrX[1], arrY[1], L"旋转前", 3);
  9. for (int i = 0; i < 4; ++i)
  10. {
  11. arrX[i] = arrX[i] * cos(st) - arrY[i] * sin(st);
  12. arrY[i] = arrX[i] * sin(st) + arrY[i] * cos(st);
  13. }
  14. polygon(arrX, arrY, 4);
  15. TextOut(g_hDc, arrX[1], arrY[1], L"旋转1后", 4);
  16. for (int i = 0; i < 4; ++i)
  17. {
  18. arrX[i] = arrX[i] * cos(st) - arrY[i] * sin(st);
  19. arrY[i] = arrX[i] * sin(st) + arrY[i] * cos(st);
  20. }
  21. polygon(arrX, arrY, 4);
  22. TextOut(g_hDc, arrX[1], arrY[1], L"旋转2后", 4);
  23. }

任意点旋转变换

  1. //任意点旋转变换
  2. void arbitPointRate(int x0, int y0, int x1, int y1, int r, int x, int y)
  3. { double k = (r*3.1415926) / 180;
  4. int a, b;
  5. MoveToEx(g_hDc,0, 0,NULL);
  6. LineTo(g_hDc,x1, y1);
  7. LineTo(g_hDc,x0, y0);
  8. LineTo(g_hDc,0, 0);
  9. = x0 - x;
  10. y0 = y0 - y;
  11. x1 = x1 - x;
  12. y1 = y1 - y;
  13. = -x;
  14. b = -y;
  15. = x0*cos(k) - y0*sin(k);
  16. y0 = x0*sin(k) + y0*cos(k);//0
  17. = x1*cos(k) - y1*sin(k);
  18. y1 = x1*sin(k) + y1*cos(k);//1
  19. = a*cos(k) - b*sin(k);
  20. b = a*sin(k) + b*cos(k);
  21. = x0 + x;
  22. y0 = y0 + y;
  23. x1 = x1 + x;
  24. y1 = y1 + y;
  25. = a + x;
  26. b = b + y;
  27. MoveToEx(g_hDc, a, b,NULL);
  28. LineTo(g_hDc, x1, y1);
  29. LineTo(g_hDc, x0, y0);
  30. LineTo(g_hDc, a, b);
  31. }

任意线旋转变换

  1. //任意线旋转变换
  2. void arbitLineRate(int* m)
  3. {
  4. int arrX[4], arrY[4];
  5. arrX[0] = 1.5*m[0] + 0.5*m[3];
  6. arrY[0] = 0.5*m[0] + 1.5*m[3];
  7. arrX[1] = 1.5*m[0] + 0.5*m[1];
  8. arrY[1] = 0.5*m[0] + 1.5*m[1];
  9. arrX[2] = 1.5*m[2] + 0.5*m[1];
  10. arrY[2] = 0.5*m[2] + 1.5*m[1];
  11. arrX[3] = 1.5*m[2] + 0.5*m[3];
  12. arrY[3] = 0.5*m[2] + 1.5*m[3];
  13. polygon(arrX,arrY,4);
  14. }

一次bezier曲线

  1. //一次bezier曲线
  2. void bezier1()
  3. {
  4. Pt3D p[2] = { { 80, 40, 0 }, { 10, 90, 0 } };
  5. Pt3D pCurr;
  6. TextOut(g_hDc, p[0].x, p[0].y, L"一次bezier曲线", 10);
  7. line(p[0].x, p[0].y, p[1].x, p[1].y);
  8. for (float i = 0; i < 1; i += 0.001) //精度0.01(100个点)
  9. {
  10. pCurr.x = (1 - i)*(p[0].x) + i*p[1].x;
  11. pCurr.y = (1 - i)*(p[0].y) + i*p[1].y;
  12. SetPixel(g_hDc,pCurr.x, pCurr.y, RGB(0, 0, 0));
  13. }
  14. }

二次bezier曲线

  1. //二次bezier曲线
  2. void bezier2()
  3. {
  4. Pt3D p[3] = { { 180, 40, 0 }, { 110, 90, 0 }, { 310, 90, 0 } };
  5. Pt3D pCurr;
  6. TextOut(g_hDc, p[0].x, p[0].y, L"二次bezier曲线", 10);
  7. line(p[0].x, p[0].y, p[1].x, p[1].y);
  8. line(p[1].x, p[1].y, p[2].x, p[2].y);
  9. for (float i = 0; i < 1; i += 0.001) //精度0.01(100个点)
  10. {
  11. pCurr.x = (p[2].x - 2 * p[1].x + p[0].x)*i*i + 2 * (p[1].x - p[0].x)*i + p[0].x;
  12. pCurr.y = (p[2].y - 2 * p[1].y + p[0].y)*i*i + 2 * (p[1].y - p[0].y)*i + p[0].y;
  13. putpixel(pCurr.x, pCurr.y, RGB(0, 0, 0));
  14. }
  15. }

三次bezier曲线

  1. //三次bezier曲线
  2. void bezier3()
  3. {
  4. Pt3D p[4] = { { 180, 150, 0 }, { 110, 200, 0 }, { 310, 200, 0 }, { 200, 340, 0 } };
  5. Pt3D pCurr;
  6. TextOut(g_hDc, p[0].x, p[0].y, L"三次bezier曲线", 10);
  7. line(p[0].x, p[0].y, p[1].x, p[1].y);
  8. line(p[1].x, p[1].y, p[2].x, p[2].y);
  9. line(p[2].x, p[2].y, p[3].x, p[3].y);
  10. for (float i = 0; i < 1; i += 0.001) //精度0.01(100个点)
  11. {
  12. pCurr.x = pow(1 - i, 3)*(p[0].x) + 3 * i*pow(1 - i, 2)*p[1].x + 3 * i*i*(1 - i)*p[2].x + i*i*i*p[3].x;
  13. pCurr.y = pow(1 - i, 3)*(p[0].y) + 3 * i*pow(1 - i, 2)*p[1].y + 3 * i*i*(1 - i)*p[2].y + i*i*i*p[3].y;
  14. putpixel(pCurr.x, pCurr.y, RGB(0, 0, 0));
  15. }
  16. }

三次B样条曲线

  1. //三次B样条曲线
  2. void thirdBCurve()
  3. {
  4. int p1[2] = { 20, 20 };
  5. int p2[2] = { 20, -10 };
  6. int p3[2] = { 10, -10 };
  7. int pCurr[2];
  8. TextOut(g_hDc, p1[0], p1[1], L"三次B样条曲线", 7);
  9. //line(p1[0], p1[1], p2[0], p2[1]);
  10. //line(p2[0], p2[1], p3[0], p3[1]);
  11. for (double i = 0; i < 1; i += 0.01)
  12. {
  13. pCurr[0] = (-pow(i, 3) + 3 * i*i - 3 * i + 1)*p1[0] + (3 * pow(i, 3) - 6 * i*i + 4)*p2[0]
  14. + (-3 * pow(i, 3) * 3 * i*i + 3 * i + 1)*p3[0];
  15. pCurr[1] = (-pow(i, 3) + 3 * i*i - 3 * i + 1)*p1[1] + (3 * pow(i, 3) - 6 * i*i + 4)*p2[1]
  16. + (-3 * pow(i, 3) * 3 * i*i + 3 * i + 1)*p3[1];
  17. putpixel(pCurr[0], pCurr[1], RGB(0, 0, 0));
  18. }
  19. }

Hermite三次样条曲线

  1. //Hermite三次样条曲线
  2. void thirdHermite()
  3. { int p1[2] = { -200, 200 };
  4. int p2[2] = { 200, -300 };
  5. int p3[2] = { 100, -200 };
  6. int pCurr[2];
  7. float h[4];
  8. TextOut(g_hDc, p1[0], p1[1], L"三次Hermite样条曲线", 13);
  9. for (float t = 0; t < 1; t += 0.01)
  10. { h[0] = 2 * t*t*t - 3 * t*t + 1;
  11. = -2 * t*t*t + 3 * t*t;
  12. = t*t*t - 2 * t*t + t;
  13. = t*t*t - t*t;
  14. pCurr[0] = p1[0] * h[0] + p1[0] * h[1] + p3[0] * h[2] + p3[0] * h[3];
  15. pCurr[1] = p1[1] * h[0] + p1[1] * h[1] + p3[1] * h[2] + p3[1] * h[3];
  16. SetPixel(g_hDc, pCurr[0], pCurr[1], RGB(255, 0, 0));
  17. }
  18. }

曲线的拼接设计

  1. // 曲线的拼接设计
  2. void compCurve()
  3. {
  4. CPoint P1, P2, P3, P4;
  5. = -50;
  6. P1.y = 50;
  7. = -100;
  8. P2.y = -200;
  9. = 160;
  10. P3.y = 40;
  11. MoveToEx(g_hDc,P1.x,P1.y,0);
  12. LineTo(g_hDc,P2.x,P2.y);
  13. LineTo(g_hDc, P3.x,P3.y);
  14. CPoint P;
  15. float t;
  16. int i;
  17. for (i = 0; i <= 400; i++)
  18. {
  19. = (float)(i / 400.0);//400.0必须是小数
  20. = (P1.x*pow(1 - t, 2) + 2 * t*(1 - t)*P2.x + t*t*P3.x);
  21. = (P1.y*pow(1 - t, 2) + 2 * t*(1 - t)*P2.y + t*t*P3.y);
  22. SetPixel(g_hDc,P.x, P.y, RGB(255, 0, 255));
  23. }
  24. CPoint P5, P6, P7, P8;
  25. = 160;
  26. P5.y = 40;
  27. = 220;
  28. P6.y = 150;
  29. = 290;
  30. P7.y = 10;
  31. = 370, P8.y = 170;
  32. MoveToEx(g_hDc,P5.x,P5.y,0);
  33. LineTo(g_hDc,P6.x,P6.y);
  34. LineTo(g_hDc, P7.x, P7.y);
  35. LineTo(g_hDc, P8.x, P8.y);
  36. for (i = 0; i <= 400; i++)
  37. {
  38. = (float)(i / 400.0);//400.0必须是小数
  39. = (int)(P5.x*pow(1 - t, 3) + P6.x * 3 * t*pow(1 - t, 2) + P7.x * 3 * t*t*(1 - t) + P8.x*pow(t, 3));
  40. = (int)(P5.y*pow(1 - t, 3) + P6.y * 3 * t*pow(1 - t, 2) + P7.y * 3 * t*t*(1 - t) + P8.y*pow(t, 3));
  41. SetPixel(g_hDc,P.x, P.y, RGB(0, 0, 255));
  42. }
  43. }

图形的填充

  1. //图形的填充
  2. void fillRect(int x0,int y0, int x1, int y1)
  3. {
  4. HBRUSH hBrush;
  5. hBrush = (HBRUSH)GetStockObject(DKGRAY_BRUSH);
  6. CRect area = { x0, y0, x1, y1 };
  7. FillRect(g_hDc,&area, hBrush);
  8. }

上传的附件 cloud_download 《图形学》基本算法实现.7z ( 200.52kb, 4次下载 )

发送私信

我连自己都猜不透,怎敢奢求别人来懂我

7
文章数
13
评论数
最近文章
eject