//HSV /** * Set Hue [0..1], Saturation [0..1] and Value [0..1] */ public void SetHSV(float hue, float saturation, float brightness, float alpha = 1.0f) { hue = CgMath.Saturate(hue); saturation = CgMath.Saturate(saturation); brightness = CgMath.Saturate(brightness); float hseg = 6 * hue; float c = saturation * brightness; float x = c * (1 - Math.Abs(hseg % 2 - 1)); int i = (int)Math.Floor(hseg); switch (i) { case 0: R = c; G = x; B = 0; break; case 1: R = x; G = c; B = 0; break; case 2: R = 0; G = c; B = x; break; case 3: R = 0; G = x; B = c; break; case 4: R = x; G = 0; B = c; break; default: R = c; G = 0; B = x; break; } float m = brightness - c; R += m; G += m; B += m; A = alpha; }
/** * Returns the point on this line segment that is closest to the vector. */ public Vector2D SnapDelta(Vector2D v) { Vector2D toEnd = End - Start; float sr = toEnd.Dot(v - Start) / toEnd.LengthSquared; return(Start + toEnd * CgMath.Saturate(sr) - v); }
public float DistanceSquared(LineSegment2D other) { //see IsIntersecting... Vector2D v = StartToEnd; Vector2D ov = other.StartToEnd; Vector2D s = Start - other.Start; float denom = v.Cross(ov); if (denom == 0) //lines are parallel { float d = Direction.OrthogonalizedClockwise().Dot(s); return(d * d); } float a = CgMath.Saturate(ov.Cross(s) / denom); float b = CgMath.Saturate(v.Cross(s) / denom); return(Math.Min(other.DistanceSquared(Sample(a)), this.DistanceSquared(other.Sample(b)))); }