Írjuk programot, amely két kör helyzetét megvizsgálja, kijelzi az állapotukat (metszik egymást, vagy érintik illetve nem érintik egymást) és grafikusan is ábrázolja.
A feladat megoldása
namespace Kör2
{
public struct t
{
public static int xx1, yy1, rr1, xx2, yy2, rr2;
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static double Távolság(double x1, double y1,
double x2, double y2)
{
return (Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) *
(y2 - y1)));
}
static int Mettszés(double x1, double y1, double r1,
double x2, double y2, double r2)
{
double d;
d = Távolság(x1, y1, x2, y2);
if (d < Math.Abs(r1 - r2)) return 1;
if (d > (r1 + r2)) return 1;
else if (d < (r1 + r2)) return 2;
else return 3;
}
static void Mettszéspont(int metsz,
double x1, double y1, double r1,
double x2, double y2, double r2,
out double x1m, out double y1m,
out double x2m, out double y2m,
out double mx, out double my)
{
double fi, z, tangfi, cosalfa;
x1m = 0; y1m = 0; x2m = 0; y2m = 0;
double c, alfa;
mx = 0; my = 0;
if (x1 > x2)
{
c = x1; x1 = x2; x2 = c;
c = y1; y1 = y2; y2 = c;
c = r1; r1 = r2; r2 = c;
}
switch (metsz)
{
case 1: break;
case 2:
tangfi = (y2 - y1) / (x2 - x1);
fi = Math.Atan(tangfi);
z = Távolság(x1, y1, x2, y2);
cosalfa = (r1 * r1 + z * z - r2 * r2) /
(2 * r1 * z);
alfa = Math.Atan(Math.Sqrt(1 - cosalfa * cosalfa)
/Math.Abs(cosalfa));
x1m = x1 + r1 * Math.Cos(alfa + fi);
y1m = y1 + r1 * Math.Sin(alfa + fi);
x2m = x1 + r1 * Math.Cos(alfa - fi);
y2m = y1 - r1 * Math.Sin(alfa - fi);
break;
case 3:
alfa = Math.Atan((y2 - y1) / (x2 - x1));
my = y1 + r1 * Math.Sin(alfa);
mx = x1 + r1 * Math.Cos(alfa);
break;
}
}
private void Vizsgálat_Click(object sender, EventArgs e)
{
double x1, y1, r1, x2, y2, r2;
double x1m, y1m, x2m, y2m, mx = 0, my = 0;
int metszés0;
if (textBox1.Text != "" && textBox2.Text != "" &&
textBox3.Text != "" && textBox4.Text != "" &&
textBox5.Text != "" && textBox6.Text != "")
{
x1 = Double.Parse(textBox1.Text);
y1 = Double.Parse(textBox2.Text);
r1 = Double.Parse(textBox3.Text);
x2 = Double.Parse(textBox4.Text);
y2 = Double.Parse(textBox5.Text);
r2 = Double.Parse(textBox6.Text);
t.xx1 = (int)x1;
t.yy1 = (int)y1;
t.rr1 = (int)r1;
t.xx2 = (int)x2;
t.yy2 = (int)y2;
t.rr2 = (int)r2;
metszés0 = Mettszés(x1, y1, r1, x2, y2, r2);
switch (metszés0)
{
case 1:
textBox13.Text =
"A köröknek nincs közös pontjuk.";
break;
case 2:
textBox13.Text = "A körök metszik egymást.";
break;
case 3:
textBox13.Text = "A körök érintik egymást.";
break;
}
Mettszéspont(metszés0, x1, y1, r1, x2, y2, r2,
out x1m,out y1m, out x2m, out y2m,
out mx, out my);
if (metszés0 == 2)
{
textBox7.Text = x1m.ToString();
textBox8.Text = y1m.ToString();
textBox9.Text = x2m.ToString();
textBox10.Text = y2m.ToString();
}
if (metszés0 == 3)
{
textBox11.Text = mx.ToString();
textBox12.Text = my.ToString();
}
Rajzolás.Enabled = true;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox7.Text = ""; textBox8.Text = "";
textBox9.Text = ""; textBox10.Text = "";
textBox11.Text = ""; textBox12.Text = "";
textBox13.Text = ""; Rajzolás.Enabled = false;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
textBox7.Text = ""; textBox8.Text = "";
textBox9.Text = ""; textBox10.Text = "";
textBox11.Text = ""; textBox12.Text = "";
textBox13.Text = ""; Rajzolás.Enabled = false;
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox7.Text = ""; textBox8.Text = "";
textBox9.Text = ""; textBox10.Text = "";
textBox11.Text = ""; textBox12.Text = "";
textBox13.Text = ""; Rajzolás.Enabled = false;
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
textBox7.Text = ""; textBox8.Text = "";
textBox9.Text = ""; textBox10.Text = "";
textBox11.Text = ""; textBox12.Text = "";
textBox13.Text = ""; Rajzolás.Enabled = false;
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
textBox7.Text = ""; textBox8.Text = "";
textBox9.Text = ""; textBox10.Text = "";
textBox11.Text = ""; textBox12.Text = "";
textBox13.Text = ""; Rajzolás.Enabled = false;
}
private void textBox6_TextChanged(object sender, EventArgs e)
{
textBox7.Text = ""; textBox8.Text = "";
textBox9.Text = ""; textBox10.Text = "";
textBox11.Text = ""; textBox12.Text = "";
textBox13.Text = ""; Rajzolás.Enabled = false;
}
private void Rajzolás_Click(object sender, EventArgs e)
{
new Form2().ShowDialog();
}
private void Form1_Load(object sender, EventArgs e)
{
Rajzolás.Enabled = false;
}
}
}
namespace Kör2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Rajzol_Click(object sender, EventArgs e)
{
int ymax = 250;
int x1, x2, y1, y2;
y1 = t.yy1; y2 = t.yy2;
x1 = t.xx1 - t.rr1; y1 = ymax - y1 -t.rr1;
x2 = t.xx2 - t.rr2; y2 = ymax - y2 - t.rr2;
Pen p1 = new Pen(Color.Blue, 2);
Pen p2 = new Pen(Color.Red, 2);
Graphics gr = CreateGraphics();
Rectangle t1 = new Rectangle(new Point(x1, y1),
new Size(2 * t.rr1, 2 * t.rr1));
Rectangle t2 = new Rectangle(new Point(x2, y2),
new Size(2 * t.rr2, 2 * t.rr2));
gr.DrawEllipse(p1, t1);
gr.DrawEllipse(p2, t2);
gr.Flush();
}
private void Bezár_Click(object sender, EventArgs e)
{
Close();
}
}
}
A program futási eredményei: