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); }
public List <PointF> GetLinerPointsFromLinearEquation(LinearEquation linear) { var x1 = this._AxisX.Max; var y2 = this._AxisY.Max; var x3 = 0 - this._AxisX.Max; var y4 = 0 - this._AxisY.Max; List <PointF> lstTmp = new List <PointF>(); int x = 0; while (x < x1) { lstTmp.Add(new PointF(x, linear.GetValueFromX(x))); x += 1; } return(lstTmp); }
private async void button12_Click(object sender, EventArgs e) { if (this.uC_Line1.IsValid()) { var a = this.uC_Line1.A; var b = this.uC_Line1.B; var c = this.uC_Line1.C; //判断方程的参数,是否有效 LinearEquation linear = new LinearEquation() { A = a, B = b, C = c }; if (!linear.IsValid()) { MessageBox.Show("输入的方程参数无效"); return; } if (!this.axisControl1.CheckLineIsValid(linear)) { MessageBox.Show("输入的方程不在坐标轴内"); return; } int x = -axisControl1.AxisY.Max; List <PointF> lstPoints = new List <PointF>(); int acc = 1; double t = 1; while (x < axisControl1.AxisY.Max) { float y = linear.GetValueFromX(x); lstPoints.Add(new PointF(x, y)); t += 1; x = (int)(acc * t * t / 2); } foreach (var ele in lstPoints) { //axisControl1.GeneratePoint(ele); axisControl1.MoveButton(ele); await Task.Delay(30); } } }
/// <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都满足条件,才是有效的 }