Beispiel #1
0
        public bool GenerateLinear(LinearEquation linear)
        {
            Color    lineColor = Color.Blue;//线条的颜色
            Graphics g         = this.CreateGraphics();
            //分别获取两个端点的值,连成线即可
            var           x1     = this._AxisX.Max;
            var           y2     = this._AxisY.Max;
            var           x3     = 0 - this._AxisX.Max;
            var           y4     = 0 - this._AxisY.Max;
            var           y1     = linear.GetValueFromX(x1);
            var           x2     = linear.GetValueFromY(y2);
            var           y3     = linear.GetValueFromX(x3);
            var           x4     = linear.GetValueFromY(y4);
            var           point1 = new PointF(x1, y1);
            var           point2 = new PointF(x2, y2);
            var           point3 = new PointF(x3, y3);
            var           point4 = new PointF(x4, y4);
            List <PointF> lstTmp = new List <PointF>()
            {
                point1, point2, point3, point4
            };
            List <PointF> lstPoint = new List <PointF>();

            foreach (PointF point in lstTmp)
            {
                //判断点是否合理
                if (CheckPointIsValid(point))
                {
                    lstPoint.Add(point);
                }
            }
            if (lstPoint.Count() < 2)
            {
                //如果点的个数小于2,不能绘制直线
                return(false);
            }
            //将刻度点,转换成像素点
            List <PointF> lstPixlsPoint = ConvertScaleToPixls(lstPoint);

            g.DrawLine(new Pen(lineColor, lineWidth), lstPixlsPoint[0], lstPixlsPoint[1]);
            g.DrawString(string.Format("L{0}", index), t_Font, Brushes.Black, new PointF(lstPixlsPoint[1].X + 2, lstPixlsPoint[1].Y - 2));
            this.label1.Text += string.Format("L{0}:{1}x+{2}y+{3}=0 ; ", index, linear.A, linear.B, linear.C);
            index++;
            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// 判断直线方程是否在坐标轴范围内
        /// </summary>
        /// <param name="linear"></param>
        /// <returns></returns>
        public bool CheckLineIsValid(LinearEquation linear)
        {
            bool flagX = false;
            bool flagY = false;
            var  y     = linear.GetValueFromX(0f);

            //判断y坐标的值有没有越界
            if (y == float.MaxValue)
            {
                //表示是垂直于x轴的直线
                var x0 = -linear.C * 1.0f / linear.A;
                if (x0 >= 0 - this._AxisX.Max && x0 < this._AxisX.Max)
                {
                    flagY = true;
                }
                else
                {
                    flagY = false;
                }
            }
            else
            {
                if (y <= this._AxisY.Max && y >= 0 - this._AxisY.Max)
                {
                    flagY = true;
                }
                else
                {
                    flagY = false;
                }
            }
            //判断x坐标的值
            var x = linear.GetValueFromY(0f);

            if (x == float.MaxValue)
            {
                var y0 = -linear.C * 1.0f / linear.B;

                if (y0 <= this._AxisY.Max && y0 >= 0 - this._AxisY.Max)
                {
                    flagX = true;
                }
                else
                {
                    flagX = false;
                }
            }
            else
            {
                if (x <= this._AxisX.Max && x >= 0 - this._AxisX.Max)
                {
                    flagX = true;
                }
                else
                {
                    flagX = false;
                }
            }

            return(flagX && flagY);//只有x,y都满足条件,才是有效的
        }