NewTOAPIA.Drawing.ClipLiangBarsky.clip_liang_barsky C# (CSharp) Method

clip_liang_barsky() public static method

public static clip_liang_barsky ( int x1, int y1, int x2, int y2, RectangleI clip_box, int x, int y ) : int
x1 int
y1 int
x2 int
y2 int
clip_box RectangleI
x int
y int
return int
        public static int clip_liang_barsky(int x1, int y1, int x2, int y2,
                                          RectangleI clip_box,
                                          int[] x, int[] y)
        {
            int XIndex = 0;
            int YIndex = 0;
            double nearzero = 1e-30;

            double deltax = x2 - x1;
            double deltay = y2 - y1;
            double xin;
            double xout;
            double yin;
            double yout;
            double tinx;
            double tiny;
            double toutx;
            double touty;
            double tin1;
            double tin2;
            double tout1;
            int np = 0;

            if (deltax == 0.0)
            {
                // bump off of the vertical
                deltax = (x1 > clip_box.x1) ? -nearzero : nearzero;
            }

            if (deltay == 0.0)
            {
                // bump off of the horizontal 
                deltay = (y1 > clip_box.y1) ? -nearzero : nearzero;
            }

            if (deltax > 0.0)
            {
                // points to right
                xin = clip_box.x1;
                xout = clip_box.x2;
            }
            else
            {
                xin = clip_box.x2;
                xout = clip_box.x1;
            }

            if (deltay > 0.0)
            {
                // points up
                yin = clip_box.y1;
                yout = clip_box.y2;
            }
            else
            {
                yin = clip_box.y2;
                yout = clip_box.y1;
            }

            tinx = (xin - x1) / deltax;
            tiny = (yin - y1) / deltay;

            if (tinx < tiny)
            {
                // hits x first
                tin1 = tinx;
                tin2 = tiny;
            }
            else
            {
                // hits y first
                tin1 = tiny;
                tin2 = tinx;
            }

            if (tin1 <= 1.0)
            {
                if (0.0 < tin1)
                {
                    x[XIndex++] = (int)xin;
                    y[YIndex++] = (int)yin;
                    ++np;
                }

                if (tin2 <= 1.0)
                {
                    toutx = (xout - x1) / deltax;
                    touty = (yout - y1) / deltay;

                    tout1 = (toutx < touty) ? toutx : touty;

                    if (tin2 > 0.0 || tout1 > 0.0)
                    {
                        if (tin2 <= tout1)
                        {
                            if (tin2 > 0.0)
                            {
                                if (tinx > tiny)
                                {
                                    x[XIndex++] = (int)xin;
                                    y[YIndex++] = (int)(y1 + tinx * deltay);
                                }
                                else
                                {
                                    x[XIndex++] = (int)(x1 + tiny * deltax);
                                    y[YIndex++] = (int)yin;
                                }
                                ++np;
                            }

                            if (tout1 < 1.0)
                            {
                                if (toutx < touty)
                                {
                                    x[XIndex++] = (int)xout;
                                    y[YIndex++] = (int)(y1 + toutx * deltay);
                                }
                                else
                                {
                                    x[XIndex++] = (int)(x1 + touty * deltax);
                                    y[YIndex++] = (int)yout;
                                }
                            }
                            else
                            {
                                x[XIndex++] = x2;
                                y[YIndex++] = y2;
                            }
                            ++np;
                        }
                        else
                        {
                            if (tinx > tiny)
                            {
                                x[XIndex++] = (int)xin;
                                y[YIndex++] = (int)yout;
                            }
                            else
                            {
                                x[XIndex++] = (int)xout;
                                y[YIndex++] = (int)yin;
                            }
                            ++np;
                        }
                    }
                }
            }
            return np;
        }