/* bezier.c generated by valac 0.56.0, the Vala compiler
 * generated from bezier.vala, do not modify */

#include <glib.h>
#include <float.h>
#include <math.h>
#include "livechart_static.h"
#include <gee.h>
#include <glib-object.h>
#include <string.h>

#define LIVE_CHART_POLYNOMIAL_TOLERANCE 1e-6

#define _g_free0(var) ((var == NULL) ? NULL : (var = (g_free (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

VALA_EXTERN GeeList* live_chart_get_cubic_roots (gdouble C3,
                                     gdouble C2,
                                     gdouble C1,
                                     gdouble C0);
static gdouble* _double_dup (gdouble* self);

GeeList*
live_chart_find_intersections_between (LiveChartSegment* segment,
                                       LiveChartBezierCurve* bezier)
{
	gdouble ax = 0.0;
	LiveChartBezierCurve _tmp0_;
	LiveChartCoord _tmp1_;
	LiveChartBezierCurve _tmp2_;
	LiveChartCoord _tmp3_;
	LiveChartBezierCurve _tmp4_;
	LiveChartCoord _tmp5_;
	LiveChartBezierCurve _tmp6_;
	LiveChartCoord _tmp7_;
	gdouble ay = 0.0;
	LiveChartBezierCurve _tmp8_;
	LiveChartCoord _tmp9_;
	LiveChartBezierCurve _tmp10_;
	LiveChartCoord _tmp11_;
	LiveChartBezierCurve _tmp12_;
	LiveChartCoord _tmp13_;
	LiveChartBezierCurve _tmp14_;
	LiveChartCoord _tmp15_;
	gdouble bx = 0.0;
	LiveChartBezierCurve _tmp16_;
	LiveChartCoord _tmp17_;
	LiveChartBezierCurve _tmp18_;
	LiveChartCoord _tmp19_;
	LiveChartBezierCurve _tmp20_;
	LiveChartCoord _tmp21_;
	gdouble by = 0.0;
	LiveChartBezierCurve _tmp22_;
	LiveChartCoord _tmp23_;
	LiveChartBezierCurve _tmp24_;
	LiveChartCoord _tmp25_;
	LiveChartBezierCurve _tmp26_;
	LiveChartCoord _tmp27_;
	gdouble cx = 0.0;
	LiveChartBezierCurve _tmp28_;
	LiveChartCoord _tmp29_;
	LiveChartBezierCurve _tmp30_;
	LiveChartCoord _tmp31_;
	gdouble cy = 0.0;
	LiveChartBezierCurve _tmp32_;
	LiveChartCoord _tmp33_;
	LiveChartBezierCurve _tmp34_;
	LiveChartCoord _tmp35_;
	gdouble dx = 0.0;
	LiveChartBezierCurve _tmp36_;
	LiveChartCoord _tmp37_;
	gdouble dy = 0.0;
	LiveChartBezierCurve _tmp38_;
	LiveChartCoord _tmp39_;
	gdouble vx = 0.0;
	LiveChartSegment _tmp40_;
	LiveChartCoord _tmp41_;
	LiveChartSegment _tmp42_;
	LiveChartCoord _tmp43_;
	gdouble vy = 0.0;
	LiveChartSegment _tmp44_;
	LiveChartCoord _tmp45_;
	LiveChartSegment _tmp46_;
	LiveChartCoord _tmp47_;
	gdouble d = 0.0;
	LiveChartSegment _tmp48_;
	LiveChartCoord _tmp49_;
	LiveChartSegment _tmp50_;
	LiveChartCoord _tmp51_;
	GeeList* roots = NULL;
	GeeList* _tmp52_;
	GeeArrayList* results = NULL;
	GeeArrayList* _tmp53_;
	GeeList* result;
#line 6 "../src/bezier.vala"
	g_return_val_if_fail (segment != NULL, NULL);
#line 6 "../src/bezier.vala"
	g_return_val_if_fail (bezier != NULL, NULL);
#line 8 "../src/bezier.vala"
	_tmp0_ = *bezier;
#line 8 "../src/bezier.vala"
	_tmp1_ = _tmp0_.c1;
#line 8 "../src/bezier.vala"
	_tmp2_ = *bezier;
#line 8 "../src/bezier.vala"
	_tmp3_ = _tmp2_.c2;
#line 8 "../src/bezier.vala"
	_tmp4_ = *bezier;
#line 8 "../src/bezier.vala"
	_tmp5_ = _tmp4_.c3;
#line 8 "../src/bezier.vala"
	_tmp6_ = *bezier;
#line 8 "../src/bezier.vala"
	_tmp7_ = _tmp6_.c0;
#line 8 "../src/bezier.vala"
	ax = ((3 * (_tmp1_.x - _tmp3_.x)) + _tmp5_.x) - _tmp7_.x;
#line 9 "../src/bezier.vala"
	_tmp8_ = *bezier;
#line 9 "../src/bezier.vala"
	_tmp9_ = _tmp8_.c1;
#line 9 "../src/bezier.vala"
	_tmp10_ = *bezier;
#line 9 "../src/bezier.vala"
	_tmp11_ = _tmp10_.c2;
#line 9 "../src/bezier.vala"
	_tmp12_ = *bezier;
#line 9 "../src/bezier.vala"
	_tmp13_ = _tmp12_.c3;
#line 9 "../src/bezier.vala"
	_tmp14_ = *bezier;
#line 9 "../src/bezier.vala"
	_tmp15_ = _tmp14_.c0;
#line 9 "../src/bezier.vala"
	ay = ((3 * (_tmp9_.y - _tmp11_.y)) + _tmp13_.y) - _tmp15_.y;
#line 11 "../src/bezier.vala"
	_tmp16_ = *bezier;
#line 11 "../src/bezier.vala"
	_tmp17_ = _tmp16_.c0;
#line 11 "../src/bezier.vala"
	_tmp18_ = *bezier;
#line 11 "../src/bezier.vala"
	_tmp19_ = _tmp18_.c1;
#line 11 "../src/bezier.vala"
	_tmp20_ = *bezier;
#line 11 "../src/bezier.vala"
	_tmp21_ = _tmp20_.c2;
#line 11 "../src/bezier.vala"
	bx = 3 * ((_tmp17_.x - (2 * _tmp19_.x)) + _tmp21_.x);
#line 12 "../src/bezier.vala"
	_tmp22_ = *bezier;
#line 12 "../src/bezier.vala"
	_tmp23_ = _tmp22_.c0;
#line 12 "../src/bezier.vala"
	_tmp24_ = *bezier;
#line 12 "../src/bezier.vala"
	_tmp25_ = _tmp24_.c1;
#line 12 "../src/bezier.vala"
	_tmp26_ = *bezier;
#line 12 "../src/bezier.vala"
	_tmp27_ = _tmp26_.c2;
#line 12 "../src/bezier.vala"
	by = 3 * ((_tmp23_.y - (2 * _tmp25_.y)) + _tmp27_.y);
#line 14 "../src/bezier.vala"
	_tmp28_ = *bezier;
#line 14 "../src/bezier.vala"
	_tmp29_ = _tmp28_.c1;
#line 14 "../src/bezier.vala"
	_tmp30_ = *bezier;
#line 14 "../src/bezier.vala"
	_tmp31_ = _tmp30_.c0;
#line 14 "../src/bezier.vala"
	cx = 3 * (_tmp29_.x - _tmp31_.x);
#line 15 "../src/bezier.vala"
	_tmp32_ = *bezier;
#line 15 "../src/bezier.vala"
	_tmp33_ = _tmp32_.c1;
#line 15 "../src/bezier.vala"
	_tmp34_ = *bezier;
#line 15 "../src/bezier.vala"
	_tmp35_ = _tmp34_.c0;
#line 15 "../src/bezier.vala"
	cy = 3 * (_tmp33_.y - _tmp35_.y);
#line 17 "../src/bezier.vala"
	_tmp36_ = *bezier;
#line 17 "../src/bezier.vala"
	_tmp37_ = _tmp36_.c0;
#line 17 "../src/bezier.vala"
	dx = _tmp37_.x;
#line 18 "../src/bezier.vala"
	_tmp38_ = *bezier;
#line 18 "../src/bezier.vala"
	_tmp39_ = _tmp38_.c0;
#line 18 "../src/bezier.vala"
	dy = _tmp39_.y;
#line 20 "../src/bezier.vala"
	_tmp40_ = *segment;
#line 20 "../src/bezier.vala"
	_tmp41_ = _tmp40_.to;
#line 20 "../src/bezier.vala"
	_tmp42_ = *segment;
#line 20 "../src/bezier.vala"
	_tmp43_ = _tmp42_.from;
#line 20 "../src/bezier.vala"
	vx = _tmp41_.y - _tmp43_.y;
#line 21 "../src/bezier.vala"
	_tmp44_ = *segment;
#line 21 "../src/bezier.vala"
	_tmp45_ = _tmp44_.from;
#line 21 "../src/bezier.vala"
	_tmp46_ = *segment;
#line 21 "../src/bezier.vala"
	_tmp47_ = _tmp46_.to;
#line 21 "../src/bezier.vala"
	vy = _tmp45_.x - _tmp47_.x;
#line 23 "../src/bezier.vala"
	_tmp48_ = *segment;
#line 23 "../src/bezier.vala"
	_tmp49_ = _tmp48_.from;
#line 23 "../src/bezier.vala"
	_tmp50_ = *segment;
#line 23 "../src/bezier.vala"
	_tmp51_ = _tmp50_.from;
#line 23 "../src/bezier.vala"
	d = (_tmp49_.x * vx) + (_tmp51_.y * vy);
#line 25 "../src/bezier.vala"
	_tmp52_ = live_chart_get_cubic_roots ((vx * ax) + (vy * ay), (vx * bx) + (vy * by), (vx * cx) + (vy * cy), ((vx * dx) + (vy * dy)) - d);
#line 25 "../src/bezier.vala"
	roots = _tmp52_;
#line 32 "../src/bezier.vala"
	_tmp53_ = gee_array_list_new (LIVE_CHART_TYPE_COORD, (GBoxedCopyFunc) live_chart_coord_dup, (GDestroyNotify) live_chart_coord_free, NULL, NULL, NULL);
#line 32 "../src/bezier.vala"
	results = _tmp53_;
#line 234 "bezier.c"
	{
		gint i = 0;
#line 33 "../src/bezier.vala"
		i = 0;
#line 239 "bezier.c"
		{
			gboolean _tmp54_ = FALSE;
#line 33 "../src/bezier.vala"
			_tmp54_ = TRUE;
#line 33 "../src/bezier.vala"
			while (TRUE) {
#line 246 "bezier.c"
				GeeList* _tmp56_;
				gint _tmp57_;
				gint _tmp58_;
				gdouble* root = NULL;
				GeeList* _tmp59_;
				gpointer _tmp60_;
				gboolean _tmp61_ = FALSE;
				gdouble* _tmp62_;
				GeeArrayList* _tmp64_;
				gdouble* _tmp65_;
				gdouble* _tmp66_;
				gdouble* _tmp67_;
				gdouble* _tmp68_;
				gdouble* _tmp69_;
				gdouble* _tmp70_;
				LiveChartCoord _tmp71_ = {0};
#line 33 "../src/bezier.vala"
				if (!_tmp54_) {
#line 265 "bezier.c"
					gint _tmp55_;
#line 33 "../src/bezier.vala"
					_tmp55_ = i;
#line 33 "../src/bezier.vala"
					i = _tmp55_ + 1;
#line 271 "bezier.c"
				}
#line 33 "../src/bezier.vala"
				_tmp54_ = FALSE;
#line 33 "../src/bezier.vala"
				_tmp56_ = roots;
#line 33 "../src/bezier.vala"
				_tmp57_ = gee_collection_get_size ((GeeCollection*) _tmp56_);
#line 33 "../src/bezier.vala"
				_tmp58_ = _tmp57_;
#line 33 "../src/bezier.vala"
				if (!(i < _tmp58_)) {
#line 33 "../src/bezier.vala"
					break;
#line 285 "bezier.c"
				}
#line 34 "../src/bezier.vala"
				_tmp59_ = roots;
#line 34 "../src/bezier.vala"
				_tmp60_ = gee_list_get (_tmp59_, i);
#line 34 "../src/bezier.vala"
				root = (gdouble*) _tmp60_;
#line 35 "../src/bezier.vala"
				_tmp62_ = root;
#line 35 "../src/bezier.vala"
				if (((gdouble) 0) > (*_tmp62_)) {
#line 35 "../src/bezier.vala"
					_tmp61_ = TRUE;
#line 299 "bezier.c"
				} else {
					gdouble* _tmp63_;
#line 35 "../src/bezier.vala"
					_tmp63_ = root;
#line 35 "../src/bezier.vala"
					_tmp61_ = (*_tmp63_) > ((gdouble) 1);
#line 306 "bezier.c"
				}
#line 35 "../src/bezier.vala"
				if (_tmp61_) {
#line 35 "../src/bezier.vala"
					_g_free0 (root);
#line 35 "../src/bezier.vala"
					continue;
#line 314 "bezier.c"
				}
#line 36 "../src/bezier.vala"
				_tmp64_ = results;
#line 36 "../src/bezier.vala"
				_tmp65_ = root;
#line 36 "../src/bezier.vala"
				_tmp66_ = root;
#line 36 "../src/bezier.vala"
				_tmp67_ = root;
#line 36 "../src/bezier.vala"
				_tmp68_ = root;
#line 36 "../src/bezier.vala"
				_tmp69_ = root;
#line 36 "../src/bezier.vala"
				_tmp70_ = root;
#line 36 "../src/bezier.vala"
				_tmp71_.x = (((((ax * (*_tmp65_)) + bx) * (*_tmp66_)) + cx) * (*_tmp67_)) + dx;
#line 36 "../src/bezier.vala"
				_tmp71_.y = (((((ay * (*_tmp68_)) + by) * (*_tmp69_)) + cy) * (*_tmp70_)) + dy;
#line 36 "../src/bezier.vala"
				gee_abstract_collection_add ((GeeAbstractCollection*) _tmp64_, &_tmp71_);
#line 33 "../src/bezier.vala"
				_g_free0 (root);
#line 338 "bezier.c"
			}
		}
	}
#line 42 "../src/bezier.vala"
	result = (GeeList*) results;
#line 42 "../src/bezier.vala"
	_g_object_unref0 (roots);
#line 42 "../src/bezier.vala"
	return result;
#line 348 "bezier.c"
}

static gdouble*
_double_dup (gdouble* self)
{
	gdouble* dup;
#line 47 "../src/bezier.vala"
	dup = g_new0 (gdouble, 1);
#line 47 "../src/bezier.vala"
	memcpy (dup, self, sizeof (gdouble));
#line 47 "../src/bezier.vala"
	return dup;
#line 361 "bezier.c"
}

GeeList*
live_chart_get_cubic_roots (gdouble C3,
                            gdouble C2,
                            gdouble C1,
                            gdouble C0)
{
	GeeArrayList* roots = NULL;
	GeeArrayList* _tmp0_;
	gdouble c3 = 0.0;
	gdouble c2 = 0.0;
	gdouble c1 = 0.0;
	gdouble c0 = 0.0;
	gdouble a = 0.0;
	gdouble b = 0.0;
	gdouble offset = 0.0;
	gdouble discrim = 0.0;
	gdouble halfB = 0.0;
	gdouble tmp = 0.0;
	gdouble root = 0.0;
	GeeList* result;
#line 47 "../src/bezier.vala"
	_tmp0_ = gee_array_list_new (G_TYPE_DOUBLE, (GBoxedCopyFunc) _double_dup, (GDestroyNotify) g_free, NULL, NULL, NULL);
#line 47 "../src/bezier.vala"
	roots = _tmp0_;
#line 50 "../src/bezier.vala"
	c3 = C3;
#line 51 "../src/bezier.vala"
	c2 = C2 / c3;
#line 52 "../src/bezier.vala"
	c1 = C1 / c3;
#line 53 "../src/bezier.vala"
	c0 = C0 / c3;
#line 55 "../src/bezier.vala"
	a = ((3 * c1) - (c2 * c2)) / 3;
#line 56 "../src/bezier.vala"
	b = (((((2 * c2) * c2) * c2) - ((9 * c1) * c2)) + (27 * c0)) / 27;
#line 57 "../src/bezier.vala"
	offset = c2 / 3;
#line 58 "../src/bezier.vala"
	discrim = ((b * b) / 4) + (((a * a) * a) / 27);
#line 59 "../src/bezier.vala"
	halfB = b / 2;
#line 61 "../src/bezier.vala"
	tmp = (gdouble) 0;
#line 62 "../src/bezier.vala"
	root = (gdouble) 0;
#line 64 "../src/bezier.vala"
	if (fabs (discrim) <= LIVE_CHART_POLYNOMIAL_TOLERANCE) {
#line 64 "../src/bezier.vala"
		discrim = (gdouble) 0;
#line 414 "bezier.c"
	}
#line 66 "../src/bezier.vala"
	if (discrim > ((gdouble) 0)) {
#line 418 "bezier.c"
		gdouble e = 0.0;
		GeeArrayList* _tmp1_;
		gdouble _tmp2_;
#line 67 "../src/bezier.vala"
		e = sqrt (discrim);
#line 69 "../src/bezier.vala"
		tmp = (-halfB) + e;
#line 70 "../src/bezier.vala"
		if (tmp >= ((gdouble) 0)) {
#line 70 "../src/bezier.vala"
			root = pow (tmp, (gdouble) (1 / 3));
#line 430 "bezier.c"
		} else {
#line 71 "../src/bezier.vala"
			root = -pow (-tmp, (gdouble) (1 / 3));
#line 434 "bezier.c"
		}
#line 73 "../src/bezier.vala"
		tmp = (-halfB) - e;
#line 74 "../src/bezier.vala"
		if (tmp >= ((gdouble) 0)) {
#line 74 "../src/bezier.vala"
			root += pow (tmp, (gdouble) (1 / 3));
#line 442 "bezier.c"
		} else {
#line 75 "../src/bezier.vala"
			root -= pow (-tmp, (gdouble) (1 / 3));
#line 446 "bezier.c"
		}
#line 77 "../src/bezier.vala"
		_tmp1_ = roots;
#line 77 "../src/bezier.vala"
		_tmp2_ = root - offset;
#line 77 "../src/bezier.vala"
		gee_abstract_collection_add ((GeeAbstractCollection*) _tmp1_, &_tmp2_);
#line 454 "bezier.c"
	} else {
#line 78 "../src/bezier.vala"
		if (discrim < ((gdouble) 0)) {
#line 458 "bezier.c"
			gdouble distance = 0.0;
			gdouble angle = 0.0;
			gdouble _cos = 0.0;
			gdouble _sin = 0.0;
			gdouble sqrt3 = 0.0;
			GeeArrayList* _tmp3_;
			gdouble _tmp4_;
			GeeArrayList* _tmp5_;
			gdouble _tmp6_;
			GeeArrayList* _tmp7_;
			gdouble _tmp8_;
#line 79 "../src/bezier.vala"
			distance = sqrt ((-a) / 3);
#line 80 "../src/bezier.vala"
			angle = atan2 (sqrt (-discrim), -halfB) / 3;
#line 81 "../src/bezier.vala"
			_cos = cos (angle);
#line 82 "../src/bezier.vala"
			_sin = sin (angle);
#line 83 "../src/bezier.vala"
			sqrt3 = sqrt ((gdouble) 3);
#line 85 "../src/bezier.vala"
			_tmp3_ = roots;
#line 85 "../src/bezier.vala"
			_tmp4_ = ((2 * distance) * _cos) - offset;
#line 85 "../src/bezier.vala"
			gee_abstract_collection_add ((GeeAbstractCollection*) _tmp3_, &_tmp4_);
#line 86 "../src/bezier.vala"
			_tmp5_ = roots;
#line 86 "../src/bezier.vala"
			_tmp6_ = ((-distance) * (_cos + (sqrt3 * _sin))) - offset;
#line 86 "../src/bezier.vala"
			gee_abstract_collection_add ((GeeAbstractCollection*) _tmp5_, &_tmp6_);
#line 87 "../src/bezier.vala"
			_tmp7_ = roots;
#line 87 "../src/bezier.vala"
			_tmp8_ = ((-distance) * (_cos - (sqrt3 * _sin))) - offset;
#line 87 "../src/bezier.vala"
			gee_abstract_collection_add ((GeeAbstractCollection*) _tmp7_, &_tmp8_);
#line 498 "bezier.c"
		} else {
			GeeArrayList* _tmp9_;
			gdouble _tmp10_;
			GeeArrayList* _tmp11_;
			gdouble _tmp12_;
#line 89 "../src/bezier.vala"
			if (halfB >= ((gdouble) 0)) {
#line 89 "../src/bezier.vala"
				tmp = -pow (halfB, (gdouble) (1 / 3));
#line 508 "bezier.c"
			} else {
#line 90 "../src/bezier.vala"
				tmp = pow (-halfB, (gdouble) (1 / 3));
#line 512 "bezier.c"
			}
#line 92 "../src/bezier.vala"
			_tmp9_ = roots;
#line 92 "../src/bezier.vala"
			_tmp10_ = (2 * tmp) - offset;
#line 92 "../src/bezier.vala"
			gee_abstract_collection_add ((GeeAbstractCollection*) _tmp9_, &_tmp10_);
#line 94 "../src/bezier.vala"
			_tmp11_ = roots;
#line 94 "../src/bezier.vala"
			_tmp12_ = (-tmp) - offset;
#line 94 "../src/bezier.vala"
			gee_abstract_collection_add ((GeeAbstractCollection*) _tmp11_, &_tmp12_);
#line 526 "bezier.c"
		}
	}
#line 97 "../src/bezier.vala"
	result = (GeeList*) roots;
#line 97 "../src/bezier.vala"
	return result;
#line 533 "bezier.c"
}

