Пример #1
0
        // From hue, sat, val, alpha (double 0..1)
        public static Color32 FromHSVA(double inHue, double inSaturation, double inValue, double inAlpha)
        {
            int    h  = (int)(inHue * 6);
            double f  = inHue * 6 - h;
            double pD = inValue * (1 - inSaturation);
            double qD = inValue * (1 - f * inSaturation);
            double tD = inValue * (1 - (1 - f) * inSaturation);

            byte alpha = (byte)(SMath.Clamp(inAlpha, 0, 1) * 255);
            byte p     = (byte)(SMath.Clamp(pD, 0, 1) * 255);
            byte q     = (byte)(SMath.Clamp(qD, 0, 1) * 255);
            byte t     = (byte)(SMath.Clamp(tD, 0, 1) * 255);
            byte value = (byte)(SMath.Clamp(inValue, 0, 1) * 255);

            switch (h % 6)
            {
            case 0: return(new Color32(value, t, p, alpha));

            case 1: return(new Color32(q, value, p, alpha));

            case 2: return(new Color32(p, value, t, alpha));

            case 3: return(new Color32(p, q, value, alpha));

            case 4: return(new Color32(t, p, value, alpha));

            case 5: return(new Color32(value, p, q, alpha));
            }

            return(new Color32(0, 0, 0, 0));
        }
Пример #2
0
        // Get shortest distance between two lines.
        // You can specify whether line A or B is a segment or "infinite" length.
        // When line is restricted to a segment the closest point is clamped to the line.
        // http://geomalgorithms.com/a07-_distance.html#dist3D_Segment_to_Segment
        public static double LineLineDistance(Vector3 startA, Vector3 endA, Vector3 startB, Vector3 endB, bool isSegmentA, bool isSegmentB, out double t, out double s)
        {
            Vector3 u = endA - startA;
            Vector3 v = endB - startB;
            Vector3 w = startA - startB;

            double a = u.Dot(u);
            double b = u.Dot(v);
            double c = v.Dot(v);
            double d = u.Dot(w);
            double e = v.Dot(w);
            double D = a * c - b * b;

            if (D == 0.0)
            {
                t = 0.0;
                s = (b > c ? d / b : e / c);
            }
            else
            {
                t = (b * e - c * d) / D;
                s = (a * e - b * d) / D;
            }

            // Clamp s and t to test segment vs segment distance
            if (isSegmentA)
            {
                t = SMath.Clamp(t, 0.0, u.Length());
            }
            if (isSegmentB)
            {
                s = SMath.Clamp(s, 0.0, w.Length());
            }

            Vector3 intersectionA = startA + u * t;
            Vector3 intersectionB = startB + v * s;

            return((intersectionB - intersectionA).Length());
        }
Пример #3
0
 // From r, g, b, a (double 0..1)
 public Color32(double inR, double inG, double inB, double inA)
 {
     components = new byte[] { (byte)(SMath.Clamp(inR, 0, 1) * 255), (byte)(SMath.Clamp(inG, 0, 1) * 255), (byte)(SMath.Clamp(inB, 0, 1) * 255), (byte)(SMath.Clamp(inA, 0, 1) * 255) };
 }
Пример #4
0
 // Get angle between this and 'other' vector in radians (0..2PI). 0 is {1, 0} goes clock-wise.
 public double AngleTo(Vector3 other)
 {
     return(Math.Acos(SMath.Clamp(Dot(other), -1, 1)));
 }