Рисование линий разных размеров и цветов. Управление цветом в зависимости от условия - C#
Формулировка задачи:
вопрос следующий у меня есть код в котором я рисую линии две нахожу их точки пересечения и так далее
вопрос следующий я хочу чтоб после отрисовок линий проверялось что если линии 1 длиннее
линии 2 то линия 1 будет красного (например) цвета, а линия 2 будет такого-то цвета.
А если линия 1 меньше линии 2 то другие цвета
Перепробовал разные варианты код не отрбатывается((( помогите пожалуйста.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Graphics gr;
gr = this.CreateGraphics();
Pen pn = new Pen(Color.Black, 3);
int X1 = Convert.ToInt32(this.textBox1.Text); //ввод данных в текстбокс
int Y1 = Convert.ToInt32(this.textBox2.Text); //ввод данных в текстбокс
int X2 = Convert.ToInt32(this.textBox3.Text); //ввод данных в текстбокс
int Y2 = Convert.ToInt32(this.textBox4.Text); //ввод данных в текстбокс
gr.DrawLine(pn, X1, Y1, X2, Y2);
}
private void button2_Click(object sender, EventArgs e)
{
Graphics gr;
gr = this.CreateGraphics();
Pen pn1 = new Pen(Color.Black, 3);
int X3 = Convert.ToInt32(this.textBox5.Text); //ввод данных в текстбокс
int Y3 = Convert.ToInt32(this.textBox6.Text); //ввод данных в текстбокс
int X4 = Convert.ToInt32(this.textBox8.Text); //ввод данных в текстбокс
int Y4 = Convert.ToInt32(this.textBox9.Text); //ввод данных в текстбокс
gr.DrawLine(pn1, X3, Y3, X4, Y4);
}
private void button3_Click(object sender, EventArgs e)
{
// очистка TextBox
Graphics gr;
gr = this.CreateGraphics();
gr.Clear(BackColor);
textBox7.Text = "";
textBox7.Clear();
textBox10.Text = "";
textBox10.Clear();
textBox11.Text = "";
textBox11.Clear();
textBox12.Text = "";
textBox12.Clear();
}
private void button4_Click(object sender, EventArgs e)
{
//расчет точки пересечения по формуле и вывод данных в два текстбокса
int x;
int y;
int X1 = Convert.ToInt32(textBox1.Text);
int Y1 = Convert.ToInt32(textBox2.Text);
int X2 = Convert.ToInt32(textBox3.Text);
int Y2 = Convert.ToInt32(textBox4.Text);
int X3 = Convert.ToInt32(textBox5.Text);
int Y3 = Convert.ToInt32(textBox6.Text);
int X4 = Convert.ToInt32(textBox8.Text);
int Y4 = Convert.ToInt32(textBox9.Text);
if (((X2 - X1) * (Y4 - Y3) * (X4 - X3) * (Y2 - Y1)) == 0)
return;
y = (Y1*(X2-X1)*(Y4-Y3)-X1*(Y2-Y1)*(Y4-Y3)-Y3*(X4-X3)*(Y2-Y1)+X3*(Y4-Y3)*(Y2-Y1))/((X2-X1)*(Y4-Y3)-(X4-X3)*(Y2-Y1));
x = ((y - Y1) * (X2 - X1) + X1 * (Y2 - Y1)) / (Y2 - Y1);
string s = Convert.ToString(x);
string p = Convert.ToString(y);
textBox7.Text = (s);
textBox10.Text = (p);
}
private void button5_Click(object sender, EventArgs e)
{
double X1 = Convert.ToDouble(textBox1.Text);
double X2 = Convert.ToDouble(textBox2.Text);
double Y1 = Convert.ToDouble(textBox3.Text);
double Y2 = Convert.ToDouble(textBox4.Text);
double d;
d = Math.Sqrt(Math.Pow(Y2 - Y1, 2) + Math.Pow(X2 - X1, 2)); //Math.Sqrt(...) - корень; Math.Pow(Х, 2) степень
d = Math.Round(d,2); //ограничение выводимой переменной до второго знака после запятой
string s = Convert.ToString(d);
textBox11.Text = (s);
}
private void button6_Click(object sender, EventArgs e)
{
double X3 = Convert.ToDouble(textBox5.Text);
double X4 = Convert.ToDouble(textBox6.Text);
double Y3 = Convert.ToDouble(textBox8.Text);
double Y4 = Convert.ToDouble(textBox9.Text);
double p;
p = Math.Sqrt(Math.Pow(Y4 - Y3, 2) + Math.Pow(X4 - X3, 2)); //Math.Sqrt(...) - корень; Math.Pow(Х, 2) степень
p = Math.Round(p, 2);
string s = Convert.ToString(p);
textBox12.Text = (s);
}Решение задачи: «Рисование линий разных размеров и цветов. Управление цветом в зависимости от условия»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace woLines
{
public partial class Form1 : Form
{
Panel panel;
Graphics graphics;
TextBox[] _textBox;
Label linesCrossLabel;
Pen
pen1,
pen2;
string[] _tbText = { "0", "0", "100", "100", "0", "100", "100", "0" };
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ClientSize = new System.Drawing.Size(500, 500);
panel = new Panel();
panel.Location = new Point(5, 5);
panel.Size = new Size(ClientSize.Width - 115, ClientSize.Height - 10);
Paint += new PaintEventHandler(Form1_Paint);
Controls.Add(panel);
graphics = panel.CreateGraphics();
int x = ClientSize.Width - 105, y = 5;
_textBox = new TextBox[8];
for (int i = 0; i < 8; i++)
{
TextBox tb = new TextBox()
{
Location = new Point(x, y),
Size = new Size(100, 25),
Text = _tbText[i]
};
tb.TextChanged += new EventHandler(tb_TextChanged);
_textBox[i] = tb;
Controls.Add(tb);
y += 35;
}
linesCrossLabel = new Label()
{
AutoSize = true,
Location = new Point(x, y),
Text = ""
};
Controls.Add(linesCrossLabel);
}
void tb_TextChanged(object sender, EventArgs e)
{
int x11, y11, x12, y12;
int x21, y21, x22, y22;
graphics.Clear(Color.White);
if (int.TryParse(_textBox[0].Text, out x11)
&& int.TryParse(_textBox[1].Text, out y11)
&& int.TryParse(_textBox[2].Text, out x12)
&& int.TryParse(_textBox[3].Text, out y12)
&& int.TryParse(_textBox[4].Text, out x21)
&& int.TryParse(_textBox[5].Text, out y21)
&& int.TryParse(_textBox[6].Text, out x22)
&& int.TryParse(_textBox[7].Text, out y22))
{
if (LenghtLine(x11, y11, x12, y12) > LenghtLine(x21, y21, x22, y22))
{
pen1 = new Pen(Color.Red,2);
pen2 = new Pen(Color.Green,2);
}
else
{
pen1 = new Pen(Color.Blue,2);
pen2 = new Pen(Color.Brown,2);
}
graphics.DrawLine(pen1, x11, y11, x12, y12);
graphics.DrawLine(pen2, x21, y21, x22, y22);
PointF point = XPoint(x11, y11, x12, y12, x21, y21, x22, y22);
linesCrossLabel.Text = "X= " + point.X.ToString() + "\nY= " + point.Y.ToString();
}
Invalidate();
}
double LenghtLine(int x1, int y1, int x2, int y2)
{
return Math.Sqrt(Math.Pow((x2 * x2 - x1 * x1), 2) + Math.Pow((y2 * y2 - y1 * y1), 2));
}
PointF XPoint(int x11, int y11, int x12, int y12, int x21, int y21, int x22, int y22)
{
float znamenat = (y22 - y21) * (x12 - x11) - (x22 - x21) * (y12 - y11);
float uA = ((x22 - x21) * (y11 - y21) - (y22 - y21) * (x11 - x21))
/ znamenat;
float uB = ((x12 - x11) * (y11 - y21) - (y12 - y11) * (x11 - x21))
/ znamenat;
return new PointF(x11 + uA * (x12 - x11), y11 + uA * (y12 - y11));
}
void Form1_Paint(object sender, PaintEventArgs e)
{
}
}
}