Advertisement
JademusSreg

Bézier Curve Sample

Mar 27th, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.53 KB | None | 0 0
  1. // Returns a point positioned a t% along the Bézier curve defined by the 4 points.
  2. point BezierCurvePoint (fixed t, point p0, point p1, point p2, point p3)
  3. {
  4.     fixed u = 1-t;
  5.     fixed tt = t*t;
  6.     fixed uu = u*u;
  7.     fixed uuu = uu*u;
  8.     fixed ttt = tt*t;
  9.     fixed vX = uuu*PointGetX(p0);
  10.     fixed vY = uuu*PointGetY(p0);
  11.     vX += 3*uu*t*PointGetX(p1);
  12.     vY += 3*uu*t*PointGetY(p1);
  13.     vX += 3*u*tt*PointGetX(p2);
  14.     vY += 3*u*tt*PointGetY(p2);
  15.     vX += ttt*PointGetX(p3);
  16.     vY += ttt*PointGetY(p3);
  17.     return Point(vX,vY);
  18. }
  19.  
  20. // Returns a point positioned a t% along the Bézier curve defined by 2 points between [0,0] and [1,1].
  21. point BezierFactorPoint (fixed t, point p1, point p2)
  22. {
  23.     return BezierCurvePoint(t,Point(0,0),p1,p2,Point(1,1));
  24. }
  25.  
  26. // Returns a fixed value representing the progress of the transition at step t.
  27. fixed BezierFactor (fixed t, point p1, point p2)
  28. {
  29.     return PointGetY(BezierFactorPoint(t,p1,p2));
  30. }
  31.  
  32. // Crude test script demonstrating it.
  33. void Test_Init ()
  34. {
  35.     // pseudo constants
  36.     playergroup g = PlayerGroupAll();
  37.     string image = "Assets\\Textures\\ui_battlenet_glue_checkcirclemark.dds";
  38.     int imageSize = 24;
  39.     int dialogSize = 300;
  40.     int dialog = Dialog(dialogSize+imageSize,dialogSize+imageSize,c_anchorCenter,0,0,Visible,Onscreen,NotFullscreen,"Assets\\Textures\\ui_challenge_iconborder.dds",NoTitle,NoTransparency,DefaultChannel);
  41.     point pointA = Point(0.42,0);
  42.     point pointB = Point(0.58,1);
  43.     // vars
  44.     point pointC;
  45.     fixed factor;
  46.     int offsetX;
  47.     int offsetY;
  48.     for (factor = 0.0048828125; factor < 1.0; factor+=0.0048828125)
  49.     {
  50.         pointC = GetBezierValuePoint(factor,pointA,pointB);
  51.         offsetX = FloorI(PointGetX(pointC)*dialogSize);
  52.         offsetY = FloorI(PointGetY(pointC)*dialogSize);
  53.         ImageSimple(dialog,DialogContainer,g,imageSize,imageSize,c_anchorBottomLeft,offsetX,offsetY,NotRelative,NoRelativeAnchor,NoTooltip,DefaultColor,image,Stretch,DefaultImageType,DefaultBlendMode,DefaultRotation);
  54.     }
  55.     ImageSimple(dialog,DialogContainer,g,imageSize,imageSize,c_anchorBottomLeft,0,0,NotRelative,NoRelativeAnchor,NoTooltip,DefaultColor,"Assets\\Textures\\stimsplat.dds",Stretch,DefaultImageType,DefaultBlendMode,DefaultRotation);
  56.     ImageSimple(dialog,DialogContainer,g,imageSize,imageSize,c_anchorTopRight,0,0,NotRelative,NoRelativeAnchor,NoTooltip,DefaultColor,"Assets\\Textures\\stimsplat.dds",Stretch,DefaultImageType,DefaultBlendMode,DefaultRotation);
  57. }
  58. // It draws this image: http://i.imgur.com/2nRuB.jpg
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement