Advertisement
nastiia_firefly

Bezier Curve for n

Nov 13th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.19 KB | None | 0 0
  1. //---------------------------------------------------------------------------
  2.  
  3. #include <vcl.h>
  4. #include <vector>
  5. #include <math.h>
  6. #pragma hdrstop
  7.  
  8. #include "Unit1.h"
  9. std::vector<POINT> points;
  10.  
  11. //---------------------------------------------------------------------------
  12. #pragma package(smart_init)
  13. #pragma resource "*.dfm"
  14. TForm1 *Form1;
  15. //---------------------------------------------------------------------------
  16. __fastcall TForm1::TForm1(TComponent* Owner)
  17.         : TForm(Owner)
  18. {
  19. }
  20. //----------------------------GLOBAL-----------------------------------------
  21. //---------------------------------------------------------------------------
  22.  
  23.         int a[100],b[100]; //points coordinates
  24.         int i=0;
  25.         int m=0;
  26.  
  27.         double t=0.0001, tk=0.0001;
  28.  
  29.         float x=0,y=0;
  30.         float BComb=1;
  31.  
  32.         POINT position;
  33.  
  34. //-------------------------DrawEllipse---------------------------------------
  35. //---------------------------------------------------------------------------
  36.  
  37.  void DrawEllipse(int x, int y)
  38. {
  39.         Form1->Image1->Canvas->Pen->Color = clTeal;
  40.         Form1->Image1->Canvas->Ellipse(x -4, y - 4, x + 4, y + 4);
  41. }
  42.  
  43. //---------------------------Factorial---------------------------------------
  44. //---------------------------------------------------------------------------
  45. int Fact(int i)
  46. {
  47.         return (i<=1)? 1 : i*Fact(i-1);
  48. }
  49. //---------------------------Bernstein---------------------------------------
  50. //---------------------------------------------------------------------------
  51. float  Bernstein(float t, int i)
  52. {
  53.         return (pow(t,i)*pow(1-t,m-i-1))*(Fact(m-1))/((Fact(i)*Fact(m-1-i)));
  54. }
  55. //--------------------------Image1Click--------------------------------------
  56. //---------------------------------------------------------------------------
  57. void __fastcall TForm1::Image1Click(TObject *Sender)
  58. {
  59.         GetCursorPos(&position);
  60.  
  61.         position.x -= (Form1->Left+10);
  62.         position.y -= (Form1->Top+30);
  63.  
  64.         points.push_back(position);
  65.         DrawEllipse(position.x, position.y);
  66.  
  67.          a[i]=position.x;
  68.          b[i]=position.y;
  69.  
  70.          i++;
  71.          m++;
  72.  
  73. }
  74. //-----------------------Button1Click---------------------------------------
  75. //---------------------------------------------------------------------------
  76. void __fastcall TForm1::Button1Click(TObject *Sender)
  77. {
  78.  
  79.         Image1->Canvas->Pen->Style=psDot;
  80.         Form1->Image1->Canvas->Pen->Color = clPurple;
  81.         Form1->Image1->Canvas->MoveTo(a[0], b[0]);
  82.  
  83.         for(int i = 0; i < m; i++)
  84.         {
  85.                 Form1->Image1->Canvas->LineTo(a[i], b[i]);
  86.         }
  87.         Form1->Image1->Canvas->Pen->Style=psSolid;
  88.  
  89. }
  90. //-----------------------Button2Click---------------------------------------
  91. //---------------------------------------------------------------------------
  92. void __fastcall TForm1::Button2Click(TObject *Sender)
  93. {
  94.        /* if(ColorDialog1->Execute())
  95.           Form1->Canvas->Pen->Color=ColorDialog1->Color;
  96.         */
  97.  
  98.         Form1->Image1->Canvas->Pen->Style=psSolid;
  99.         Form1->Image1->Canvas->Pen->Color = clSkyBlue;
  100.         Form1->Image1->Canvas->MoveTo(a[0], b[0]);
  101.  
  102.         for(t=0;t<1;t+=tk)
  103.         {
  104.                  float  x=pow((1-t),3)*a[0] + 3*pow((1-t),2)*t*a[1]
  105.                             +3*(1-t)*pow(t,2)*a[2] + pow(t,3)*a[3];
  106.  
  107.                  float  y=pow((1-t),3)*b[0] + 3*pow((1-t),2)*t*b[1]
  108.                             +3*(1-t)*pow(t,2)*b[2] + pow(t,3)*b[3];
  109.  
  110.         Form1->Image1->Canvas->LineTo(x, y);
  111.         }
  112. }
  113. //-----------------------Button3Click---------------------------------------
  114. //---------------------------------------------------------------------------
  115. void __fastcall TForm1::Button3Click(TObject *Sender)
  116. {
  117.         /* if(ColorDialog1->Execute())
  118.           Form1->Canvas->Pen->Color=ColorDialog1->Color;
  119.         */
  120.  
  121.         x=0;y=0;
  122.          for(t=0.00001;t<1;t+=tk)
  123.          {
  124.                  for(int i=0;i<m;i++)
  125.                  {
  126.                         x+=Bernstein(t,i)*a[i];
  127.                         y+=Bernstein(t,i)*b[i];
  128.                  }
  129.  
  130.                 Form1->Image1->Canvas->Pixels[x][y]=clBlack;
  131.                 x=0;y=0;
  132.         }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement