1998-10-20 00:46:58 +04:00
|
|
|
//
|
2021-11-16 02:02:16 +03:00
|
|
|
// Bézier curve functions for the Fast Light Tool Kit (FLTK).
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
2010-11-29 00:06:39 +03:00
|
|
|
// Copyright 1998-2010 by Bill Spitzak and others.
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/COPYING.php
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2005-04-16 04:13:17 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/bugs.php
|
1998-10-20 00:46:58 +04:00
|
|
|
//
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2008-10-15 01:42:24 +04:00
|
|
|
/**
|
|
|
|
\file fl_curve.cxx
|
2021-11-16 02:02:16 +03:00
|
|
|
\brief Utility for drawing Bézier curves, adding the points to the
|
2008-10-15 01:42:24 +04:00
|
|
|
current fl_begin/fl_vertex/fl_end path.
|
|
|
|
|
|
|
|
Incremental math implementation:
|
|
|
|
I very much doubt this is optimal! From Foley/vanDam page 511.
|
2008-12-07 19:16:11 +03:00
|
|
|
If anybody has a better algorithm, please send it!
|
2008-10-15 01:42:24 +04:00
|
|
|
*/
|
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
#include <FL/fl_draw.H>
|
|
|
|
#include <math.h>
|
|
|
|
|
2018-06-23 19:47:40 +03:00
|
|
|
/**
|
2018-06-23 23:50:22 +03:00
|
|
|
\cond DriverDev
|
|
|
|
\addtogroup DriverDeveloper
|
|
|
|
\{
|
2018-06-23 19:47:40 +03:00
|
|
|
*/
|
|
|
|
|
2016-04-24 11:38:11 +03:00
|
|
|
/** see fl_curve() */
|
2010-05-27 21:20:18 +04:00
|
|
|
void Fl_Graphics_Driver::curve(double X0, double Y0,
|
2020-07-01 19:03:10 +03:00
|
|
|
double X1, double Y1,
|
|
|
|
double X2, double Y2,
|
|
|
|
double X3, double Y3) {
|
2001-01-28 09:57:33 +03:00
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
double x = fl_transform_x(X0,Y0);
|
|
|
|
double y = fl_transform_y(X0,Y0);
|
2001-01-28 09:57:33 +03:00
|
|
|
|
|
|
|
// draw point 0:
|
|
|
|
fl_transformed_vertex(x,y);
|
|
|
|
|
1998-10-06 22:21:25 +04:00
|
|
|
double x1 = fl_transform_x(X1,Y1);
|
2022-12-01 00:40:52 +03:00
|
|
|
double y1 = fl_transform_y(X1,Y1);
|
1998-10-06 22:21:25 +04:00
|
|
|
double x2 = fl_transform_x(X2,Y2);
|
|
|
|
double y2 = fl_transform_y(X2,Y2);
|
|
|
|
double x3 = fl_transform_x(X3,Y3);
|
|
|
|
double y3 = fl_transform_y(X3,Y3);
|
|
|
|
|
2001-01-28 09:57:33 +03:00
|
|
|
// find the area:
|
2022-12-01 00:40:52 +03:00
|
|
|
double a = fabs((x-x2)*(y3-y1)-(y-y2)*(x3-x1));
|
|
|
|
double b = fabs((x-x3)*(y2-y1)-(y-y3)*(x2-x1));
|
2001-01-28 09:57:33 +03:00
|
|
|
if (b > a) a = b;
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2001-01-28 09:57:33 +03:00
|
|
|
// use that to guess at the number of segments:
|
2019-02-02 19:47:55 +03:00
|
|
|
int nSeg = int(sqrt(a)/4);
|
|
|
|
if (nSeg > 1) {
|
|
|
|
if (nSeg > 100) nSeg = 100; // make huge curves not hang forever
|
2022-12-01 00:40:52 +03:00
|
|
|
if (nSeg < 9) nSeg = 9; // make tiny curevs look bearable
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2019-02-02 19:47:55 +03:00
|
|
|
double e = 1.0/nSeg;
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2001-01-28 09:57:33 +03:00
|
|
|
// calculate the coefficients of 3rd order equation:
|
|
|
|
double xa = (x3-3*x2+3*x1-x);
|
|
|
|
double xb = 3*(x2-2*x1+x);
|
|
|
|
double xc = 3*(x1-x);
|
|
|
|
// calculate the forward differences:
|
|
|
|
double dx1 = ((xa*e+xb)*e+xc)*e;
|
|
|
|
double dx3 = 6*xa*e*e*e;
|
|
|
|
double dx2 = dx3 + 2*xb*e*e;
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2001-01-28 09:57:33 +03:00
|
|
|
// calculate the coefficients of 3rd order equation:
|
2022-12-01 00:40:52 +03:00
|
|
|
double ya = (y3-3*y2+3*y1-y);
|
|
|
|
double yb = 3*(y2-2*y1+y);
|
|
|
|
double yc = 3*(y1-y);
|
2001-01-28 09:57:33 +03:00
|
|
|
// calculate the forward differences:
|
|
|
|
double dy1 = ((ya*e+yb)*e+yc)*e;
|
|
|
|
double dy3 = 6*ya*e*e*e;
|
|
|
|
double dy2 = dy3 + 2*yb*e*e;
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2019-02-03 11:07:22 +03:00
|
|
|
// draw points 1 .. nSeg-2:
|
2019-02-03 11:05:36 +03:00
|
|
|
for (int i=2; i<nSeg; i++) {
|
2001-01-28 09:57:33 +03:00
|
|
|
x += dx1;
|
|
|
|
dx1 += dx2;
|
|
|
|
dx2 += dx3;
|
|
|
|
y += dy1;
|
|
|
|
dy1 += dy2;
|
|
|
|
dy2 += dy3;
|
|
|
|
fl_transformed_vertex(x,y);
|
|
|
|
}
|
|
|
|
|
2019-02-03 11:05:36 +03:00
|
|
|
// draw point nSeg-1:
|
2001-01-28 09:57:33 +03:00
|
|
|
fl_transformed_vertex(x+dx1, y+dy1);
|
|
|
|
}
|
1998-10-06 22:21:25 +04:00
|
|
|
|
2019-02-03 11:07:22 +03:00
|
|
|
// draw point nSeg:
|
1998-10-06 22:21:25 +04:00
|
|
|
fl_transformed_vertex(x3,y3);
|
|
|
|
}
|
1998-10-20 00:46:58 +04:00
|
|
|
|
2018-06-23 19:47:40 +03:00
|
|
|
/**
|
2018-06-23 23:50:22 +03:00
|
|
|
\}
|
|
|
|
\endcond
|
2018-06-23 19:47:40 +03:00
|
|
|
*/
|