コード例 #1
0
 public Line(Vector vect1, Vector vect2,bool outside)
 {
     //v1 = vect1;
     //v2 = vect2;
     //Vectors = new List<Vector>() { v1, v2 };
     Vectors = new List<Vector>() { vect1, vect2 };
     this.isOutside = false;
 }
コード例 #2
0
 public Line(Vector vect1, Vector vect2)
 {
     //v1 = vect1;
     //v2 = vect2;
     //Vectors = new List<Vector>() { v1, v2 };
     Vectors = new List<Vector>() { vect1, vect2 };
     isOutside = false;
 }
コード例 #3
0
        /// <summary>
        /// Creates a new Rectangle.
        /// </summary>
        /// <param name="vect1">The top,left corner point.</param>
        /// <param name="vect2">The bottom,right corner point.</param>
        public myRectangle(Vector vect1, Vector vect2)
        {
            this.v1 = new Vector(vect1.X,vect1.Y,false);
            this.v2 = new Vector(vect2.X, vect1.Y,false);
            this.v3 = new Vector(vect2.X,vect2.Y,false);
            this.v4 = new Vector(vect1.X, vect2.Y,false);

            Vectors = new Vector[] { v1, v2, v3, v4 };
        }
コード例 #4
0
 /// <summary>
 /// Adds a Vector to this triangle.
 /// </summary>
 /// <param name="v">The vector to add.</param>
 public void AddPoint(Vector v)
 {
     this.Vectors.Add(v);
 }
コード例 #5
0
        private static bool CheckIfValidForBoth(Vector v1, Vector v2, Vector ClippingWindowV1, Vector ClippingWindowV2, float r, float s)
        {
            //v1.x + (Left1X * r)
            float check1 = v1.X + ((v2.X - v1.X) * r);

            //ClippingWindowV1.X + (Right1X * s)
            float check2 = ClippingWindowV1.X + ((ClippingWindowV2.X - ClippingWindowV1.X) * s);

            if (Convert.ToInt32(check1) == Convert.ToInt32(check2))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
コード例 #6
0
        public static Vector CalculateIntersection(Vector v1, Vector v2, Vector ClippingWindowV1, Vector ClippingWindowV2)
        {
            if (Math.Abs(v1.Y - v2.Y) == 0) //our line(v1,v2) is horizontal
            {
                if (Math.Abs(ClippingWindowV1.Y - ClippingWindowV2.Y) == 0) //horizontal line
                {
                    return new Vector(0, 0, false); //no intersection
                }
            }

            if (Math.Abs(v1.X - v2.X) == 0) //vertical line
            {
                if (Math.Abs(ClippingWindowV1.X - ClippingWindowV2.X) == 0) //vertical too
                {
                    return new Vector(0, 0, false); //no intersection
                }
            }

            //calcualte our valid ranges
            float minX = v1.X >= v2.X ? v2.X : v1.X;
            float maxX = v1.X >= v2.X ? v1.X : v2.X;

            float minY = v1.Y >= v2.Y ? v2.Y : v1.Y;
            float maxY = v1.Y >= v2.Y ? v1.Y : v2.Y;

            float ClippMaxX = ClippingWindowV1.X >= ClippingWindowV2.X ? ClippingWindowV1.X : ClippingWindowV2.X;
            float ClippMinX = ClippingWindowV1.X >= ClippingWindowV2.X ? ClippingWindowV2.X : ClippingWindowV1.X;

            float ClippMaxY = ClippingWindowV1.Y >= ClippingWindowV2.Y ? ClippingWindowV1.Y : ClippingWindowV2.Y;
            float ClippMinY = ClippingWindowV1.Y >= ClippingWindowV2.Y ? ClippingWindowV2.Y : ClippingWindowV1.Y;

            //v1 + r * v2 = ClippingWindowV1 + s * ClippingWindowV2 // to get the intersection point

            //v2 & ClippingWindowV2 sould be our directional vectors...
            // directional vector(left1X) = v2.X - v1.X // AB = OB - OA

            float Left1X = (v2.X - v1.X);
            float Left1Y = (v2.Y - v1.Y);

            float Right1X = (ClippingWindowV2.X - ClippingWindowV1.X);
            float Right1Y = ClippingWindowV2.Y - ClippingWindowV1.Y;

            //solve this...
            //x1 + r * x2 = x3 + s * x4 | -x3
            //y1 + r * y2 = y3 + s * y4 | -y3

            //leftX + r * v2.X = s * ClippingWindowV2.X
            //leftY + r * v2.Y = s * ClippingWindowV2.Y

            float leftX = v1.X - ClippingWindowV1.X;
            float leftY = v1.Y - ClippingWindowV1.Y;

            //now we have

            //leftX + r * Left1X = s * Right1X
            //leftY + r * Left1Y = s * Right1Y

            float r, s;

            #region general checks

            //leftX + r * Left1X = s * 0
            //leftY + r * 0 = s * Right1Y

            if (Left1Y == 0 && Right1X == 0) //no need to extend... //leftX + r * Left1X = 0 && s * Right1Y = 0
            {
                //leftX + r * LeftX = 0 | - leftX

                // r * Left1X = -leftX | / LeftX
                //r = (-leftX) / Left1X
                r = (-leftX) / Left1X;

                s = leftY / Right1Y;

                if (r >= 0 && r <= 1 && s >= 0 && s <= 1) //we only need valid intersections ( r > 1 would be beyond our v2 vector && r < 0 beyond v1)
                {
                    //to int because float is sometimes glitched (round)

                    //check s too

                    return new Vector(Convert.ToInt32(v1.X + Left1X * r), Convert.ToInt32(v1.Y + Left1Y * r),false);

                }
                else
                {
                    return new Vector(0, 0, false);
                }
            }

            //leftX + r * 0 = s * Right1X
            //leftY + r * Left1Y = s * 0

            if (Left1X == 0 && Right1Y == 0) //no need to extend
            {
                // leftY + r * Left1Y = 0 | - leftY

                //r * Left1Y = -leftY | / Left1Y

                r = (-leftY) / Left1Y;

                s = leftX / Right1X;

                if (r >= 0 && r <= 1 && s >= 0 && s <= 1) //we only need valid intersections ( r > 1 would be beyond our v2 vector && r < 0 beyond v1)
                {
                    //to int because float is sometimes glitched (round)

                    return new Vector(Convert.ToInt32(v1.X + Left1X * r), Convert.ToInt32(v1.Y + Left1Y * r),false );

                }
                else
                {
                    return new Vector(0, 0, false);
                }

            }

            //leftX + r * Left1X = s * Right1X
            //leftY + r * 0 = s * Right1Y

            if (Left1Y == 0)
            {
                //leftY + 0 = s * Right1Y | / Right1Y
                s = leftY / Right1Y;

                r = (s * Right1X - leftX) / Left1X;

                if (s >= 0 && s <= 1 && r >= 0 && r <= 1)  //we only need valid intersections ( r > 1 would be beyond our v2 vector && r < 0 beyond v1)
                {

                        return new Vector(Convert.ToInt32(ClippingWindowV1.X + Right1X * s), Convert.ToInt32(ClippingWindowV1.Y + Right1Y * s),false);
                }
                else
                {
                    return new Vector(0, 0, false);
                }
            }

            //leftX + r * 0 = s * Right1X
            //leftY + r * Left1Y = s * Right1Y

            if (Left1X == 0)
            {
                //leftX + r * 0 = s * Right1X | / Right1X
                s = leftX / Right1X;

                r = (s * Right1Y - leftY) / Left1Y;

                if (s >= 0 && s <= 1 && r >= 0 && r <= 1)
                {

                    return new Vector(Convert.ToInt32(ClippingWindowV1.X + Right1X * s), Convert.ToInt32(ClippingWindowV1.Y + Right1Y * s),false );

                }
                else
                {
                    return new Vector(0, 0, false);
                }
            }

            //leftX + r * Left1X = s * 0
            //leftY + r * Left1Y = s * Right1Y

            if (Right1X == 0)
            {
                //leftX + r * Left1X = 0
                r = (-leftX) / Left1X;

                s = (r * Left1Y + leftY) / Right1Y;

                if (r >= 0 && r <= 1 && s >= 0 && s <= 1)
                {

                    return new Vector(Convert.ToInt32(v1.X + Left1X * r), Convert.ToInt32(v1.Y + Left1Y * r),false );

                }
                else
                {
                    return new Vector(0, 0, false);
                }

            }

            //leftX + r * Left1X = s * Right1X
            //leftY + r * Left1Y = s * 0

            if (Right1Y == 0)
            {
                //leftY + r * Left1Y = 0

                r = (-leftY) / Left1Y;

                s = (r * Left1X + leftX) / Right1X;

                if (r >= 0 && r <= 1 && s >= 0 && s <= 1)
                {

                    return new Vector(Convert.ToInt32(v1.X + Left1X * r), Convert.ToInt32(v1.Y + Left1Y * r),false );

                }
                else
                {
                    return new Vector(0, 0, false);
                }

            }

            #endregion
            //now we need to extend

            //(leftX*Left1Y) + r * (Left1X*Left1Y) = s * (Right1X*Left1Y)
            //(leftY*Left1X) + r * (Left1Y*Left1X) = s * (Right1Y*Left1X)

            Left1X = Left1X * Left1Y;
            Left1Y = Left1Y * Left1X;

            Right1X = Right1X * Left1Y;
            Right1Y = Right1Y * Left1X;

            leftX = leftX * Left1Y;
            leftY = leftY * Left1X;

            //I.    leftX + r * 1 = s * (Right1X*Left1Y)
            //II.   leftY + r * 1 = s * (Right1Y*Left1X)

            // I - II
            float tempRight, tempLeft;

            tempRight = Right1X - Right1Y;
            tempLeft = leftX - leftY;
            //1r - 1r = 0r = 0

            //tempLeft = s * tempRight

            s = tempLeft / tempRight;

            //now check r

            //leftX + r * 1 = s * (Right1X)
            //leftX + r  = s * (Right1X) |- leftX
            // r = s * (Right1X) - leftX

            //r = ((RenderWindowPunkt2.X * s) + RenderWindowPunkt1.X - StartPunkt.X) / EndPunkt.X;
            r = (s * Right1X) - leftX;

            if (CheckIfValidForBoth(v1, v2, ClippingWindowV1, ClippingWindowV2, r, s) == true)
            {
                return new Vector(ClippingWindowV1.X + (ClippingWindowV2.X * s), ClippingWindowV1.Y + (ClippingWindowV2.Y * s),false );
            }

            return new Vector(0, 0, false);
        }
コード例 #7
0
        void logic_OnVectorClicked(Vector obj)
        {
            //this.CurrentClickedVectors.Clear();

            //this.CurrentClickedVectors.Add(obj);

            this.ClearClickedVectors();

            prevSelected.Marked = false;

            int index = logic.AllVectors.FindIndex((vect) => vect.X == obj.X && vect.Y == obj.Y);

            foreach (ListViewItem item in lvLogger.Items)
            {
                item.Selected = false;
            }

            logic.CurrentOnMainFormSelectedVector = obj; //we just want to show the Marked = true on the selected vector

            obj.Marked = true;

            if (index != -1)
            {
                lvLogger.Items[index].Selected = true;
            }

            this.AddClickedVectors(obj);

            prevSelected = obj;

            logic.CurrentOnMainFormSelectedVector = null; //now we can clear all Marked = ? states

            lvLogger.Focus(); // to show selection
        }
コード例 #8
0
        private OutcodeState CheckOutcodes(Vector v1, Vector v2)
        {
            string outcode1 = v1.Outcode.ToString("0000");
            string outcode2 = v2.Outcode.ToString("0000");

            string result = "";

            //compare via logiacal AND

            if (outcode1 == "0000" && outcode2 == "0000") // we are inside
            {
                return OutcodeState.inside;
            }

            if (outcode1 == "0000" || outcode2 == "0000") //partially
            {
                return OutcodeState.partially;
            }

            for (int i = 0; i < outcode1.Length; i++)
            {
                if (outcode1[i] == outcode2[i] && outcode1[i] == '0')
                {
                    result += "0";
                }
                else if (outcode1[i] == outcode2[i] && outcode1[i] == '1')
                {
                    result += "1";
                }
                else if (outcode1[i] == '0' && outcode2[i] == '1' || outcode1[i] == '1' && outcode2[i] == '0')
                {
                    result += "0";
                }
            }
            if (result == "0000") // 0010,0001 --> 0000 --> partially
            {
                return OutcodeState.partially;
            }

            if (result.Contains('1')) //we are outside
            {
                return OutcodeState.outside;
            }

            //hope we never get here...
            return OutcodeState.outside;
        }
コード例 #9
0
        /// <summary>
        /// Checks and calcualtes Intersection for a line.
        /// </summary>
        /// <param name="v1">The starting point.</param>
        /// <param name="v2">The ending point.</param>
        /// <param name="ClippingWindowV1">The clipping starting point.</param>
        /// <param name="ClippingWindowV2">The ending clipping point.</param>
        private void CheckIntersectionLine(Vector v1, Vector v2, Vector ClippingWindowV1, Vector ClippingWindowV2, ref List<Vector> intersectionVectors)
        {
            Vector intersectionPoint = Cutter.CalculateIntersection(v1, v2, ClippingWindowV1, ClippingWindowV2);

            if (intersectionPoint.X != 0 && intersectionPoint.Y != 0)
            {
                if (intersectionVectors.Count == 0)
                {
                    //1 intersection point
                    //Vector realVector = new Vector(intersectionPoint, Logic.Index++);
                    //Vector realVector = new Vector(intersectionPoint);

                    this.calculatedLines.Add(new Line(v1, intersectionPoint));
                    this.calculatedLines.Add(new Line(intersectionPoint, v2));

                    //if (!this.AllVectors.Exists((v) => v.X == realVector.X && v.Y == realVector.Y))
                    //{
                    //    this.AllVectors.Add(realVector);
                    //}

                    intersectionVectors.Add(intersectionPoint);
                }
                else if (intersectionVectors.Count == 1)
                {

                    //Vector realVector = new Vector(intersectionPoint, Logic.Index++);
                    //Vector realVector = new Vector(intersectionPoint);

                    //found 2. intersection point...
                    this.calculatedLines.RemoveAt(this.calculatedLines.Count - 1); //last one was wrong
                    this.calculatedLines.Add(new Line(intersectionVectors[0], intersectionPoint));
                    //right one
                    this.calculatedLines.Add(new Line(intersectionPoint, v2));

                    //if (!this.AllVectors.Exists((v) => v.X == realVector.X && v.Y == realVector.Y))
                    //{
                    //    this.AllVectors.Add(realVector);
                    //}

                    intersectionVectors.Add(intersectionPoint);
                }

            }
            else
            {
                //but check if we have this line already...

                Line tempLine = new Line(v1, v2);

                if (!this.calculatedLines.Exists((line) => line.Vectors[0].X == tempLine.Vectors[0].X && line.Vectors[0].Y == tempLine.Vectors[0].Y))
                {
                    this.calculatedLines.Add(tempLine);
                }
            }
        }
コード例 #10
0
        /// <summary>
        /// Adds a Point.
        /// </summary>
        /// <param name="x">The x koordiante.</param>
        /// <param name="y">The y koordinate.</param>
        public void AddPoint(float x, float y)
        {
            Vector v = new Vector(x, y, true);

            //lvLogger.Items.Add(new ListViewItem(new string[] { v.ID.ToString(), "X: " + v.X + " Y: " + v.Y, v.Outcode.ToString() }));

            switch (this.drawingMode)
            {
                case DrawingMode.Points:
                    this.AllVectors.Add(v);
                    this.Points.Add(v);

                    break;

                case DrawingMode.Triangles:

                    if (this._trianglePointCount == 3) // 0 - 2
                    {
                        this._trianglePointCount = 0;
                    }
                    if (this._trianglePointCount == 0) // because we started with 0
                    {
                        this.logicalTriangles.Add(new Triangle());
                    }

                    this.AllVectors.Add(v);
                    this.logicalTriangles[this.logicalTriangles.Count - 1].AddPoint(v);
                    this._trianglePointCount++;

                    break;

                case DrawingMode.Lines:

                    if (this._linePointCount == 2)
                    {
                        this._linePointCount = 0;
                    }
                    if (this._linePointCount == 0)
                    {
                        this.logicalLines.Add(new Line());
                    }

                    this.AllVectors.Add(v);
                    this.logicalLines[this.logicalLines.Count - 1].AddPoint(v);
                    this._linePointCount++;

                    break;

                case DrawingMode.None: break;

                default:
                    break;
            }

            UpdateLogger();
            Program.fmMain.Updater.Start();
        }
コード例 #11
0
 /// <summary>
 /// Fills a ellipse.
 /// </summary>
 /// <param name="g">The Graphic.</param>
 /// <param name="brush">The fill color.</param>
 /// <param name="point">The koordinates as point.</param>
 /// <param name="width">The width of the ellipse.</param>
 /// <param name="height">The height of the ellipse.</param>
 public void FillEllipse(SolidBrush brush, Vector point, float width, float height)
 {
     this.g.FillEllipse(brush, point.X - (width / 2), Parent.Height - (point.Y + (height / 2)), width, height);
 }
コード例 #12
0
 /// <summary>
 /// Draws a line.
 /// </summary>
 /// <param name="g">The Graphic.</param>
 /// <param name="pen">The line color.</param>
 /// <param name="v1">The start point.</param>
 /// <param name="v2">The end point.</param>
 public void DrawLine(Pen pen, Vector v1, Vector v2)
 {
     this.g.DrawLine(pen, v1.X, Parent.Height - v1.Y, v2.X, Parent.Height - v2.Y);
 }