本文发表在 rolia.net 枫下论坛using System.Drawing;
namespace Line2
{
public struct xPoint
{
public double X;
public double Y;
public xPoint(double _x, double _y)
{
X = _x;
Y = _y;
}
}
public struct xLine
{
public xPoint start_point;
public xPoint end_point;
public xLine(xPoint _start_point, xPoint _end_point)
{
start_point = _start_point;
end_point = _end_point;
}
}
public struct xIntersection
{
public xLine line1;
public xLine line2;
public xPoint intersection_point;
public xIntersection(xLine _line1, xLine _line2, xPoint _intersection_point)
{
line1 = _line1;
line2 = _line2;
intersection_point = _intersection_point;
}
}
class csLine
{
private xLine l1;
private xLine l2;
public csLine(xLine _l1,xLine _l2)
{
l1 = _l1;
l2 = _l2;
}
public xIntersection get_intersection_point()
{
int n = 2;
double[] x1 = new double[n];
double[] y1 = new double[n];
double[] x2 = new double[n];
double[] y2 = new double[n];
double[] a = new double[n];
double[] b = new double[n];
double[] c = new double[n];
double jx, jy;
x1[0] = l1.start_point.X;
y1[0] = l1.start_point.Y;
x2[0] = l1.end_point.X;
y2[0] = l1.end_point.Y;
x1[1] = l2.start_point.X;
y1[1] = l2.start_point.Y;
x2[1] = l2.end_point.X;
y2[1] = l2.end_point.Y;
for (int i = 0; i < n; ++i)
{
if (x1[i] == x2[i])
{
a[i] = 1;
b[i] = 0;
c[i] = -x1[i];
}
else
{
a[i] = (y2[i] - y1[i]) / (x1[i] - x2[i]);
b[i] = 1;
c[i] = (x1[i] * y2[i] - x2[i] * y1[i]) / (x2[i] - x1[i]);
}
}
xIntersection return_intersection = new xIntersection();
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
if (a[i] * b[j] - a[j] * b[i] != 0)
{
jx = (b[i] * c[j] - b[j] * c[i]) / (a[i] * b[j] - a[j] * b[i]);
jy = (c[i] * a[j] - c[j] * a[i]) / (a[i] * b[j] - a[j] * b[i]);
if (between(jx, x1[i], x2[i]) && between(jx, x1[j], x2[j]) && between(jy, y1[i], y2[i]) && between(jy, y1[j], y2[j]))
{
xLine line1 = new xLine(new xPoint(x1[i], y1[i]), new xPoint(x2[i], x2[i]));
xLine line2 = new xLine(new xPoint(x1[j], y1[j]), new xPoint(x2[j], x2[j]));
xPoint intersection_point = new xPoint(jx, jy);
return_intersection = new xIntersection(line1, line2, intersection_point);
}
}
}
}
return return_intersection;
}
private bool between(double u, double v, double w)
{
double max, min;
max = (v > w) ? v : w;
min = (v < w) ? v : w;
return u <= max && u >= min;
}
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
namespace Line2
{
public struct xPoint
{
public double X;
public double Y;
public xPoint(double _x, double _y)
{
X = _x;
Y = _y;
}
}
public struct xLine
{
public xPoint start_point;
public xPoint end_point;
public xLine(xPoint _start_point, xPoint _end_point)
{
start_point = _start_point;
end_point = _end_point;
}
}
public struct xIntersection
{
public xLine line1;
public xLine line2;
public xPoint intersection_point;
public xIntersection(xLine _line1, xLine _line2, xPoint _intersection_point)
{
line1 = _line1;
line2 = _line2;
intersection_point = _intersection_point;
}
}
class csLine
{
private xLine l1;
private xLine l2;
public csLine(xLine _l1,xLine _l2)
{
l1 = _l1;
l2 = _l2;
}
public xIntersection get_intersection_point()
{
int n = 2;
double[] x1 = new double[n];
double[] y1 = new double[n];
double[] x2 = new double[n];
double[] y2 = new double[n];
double[] a = new double[n];
double[] b = new double[n];
double[] c = new double[n];
double jx, jy;
x1[0] = l1.start_point.X;
y1[0] = l1.start_point.Y;
x2[0] = l1.end_point.X;
y2[0] = l1.end_point.Y;
x1[1] = l2.start_point.X;
y1[1] = l2.start_point.Y;
x2[1] = l2.end_point.X;
y2[1] = l2.end_point.Y;
for (int i = 0; i < n; ++i)
{
if (x1[i] == x2[i])
{
a[i] = 1;
b[i] = 0;
c[i] = -x1[i];
}
else
{
a[i] = (y2[i] - y1[i]) / (x1[i] - x2[i]);
b[i] = 1;
c[i] = (x1[i] * y2[i] - x2[i] * y1[i]) / (x2[i] - x1[i]);
}
}
xIntersection return_intersection = new xIntersection();
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
if (a[i] * b[j] - a[j] * b[i] != 0)
{
jx = (b[i] * c[j] - b[j] * c[i]) / (a[i] * b[j] - a[j] * b[i]);
jy = (c[i] * a[j] - c[j] * a[i]) / (a[i] * b[j] - a[j] * b[i]);
if (between(jx, x1[i], x2[i]) && between(jx, x1[j], x2[j]) && between(jy, y1[i], y2[i]) && between(jy, y1[j], y2[j]))
{
xLine line1 = new xLine(new xPoint(x1[i], y1[i]), new xPoint(x2[i], x2[i]));
xLine line2 = new xLine(new xPoint(x1[j], y1[j]), new xPoint(x2[j], x2[j]));
xPoint intersection_point = new xPoint(jx, jy);
return_intersection = new xIntersection(line1, line2, intersection_point);
}
}
}
}
return return_intersection;
}
private bool between(double u, double v, double w)
{
double max, min;
max = (v > w) ? v : w;
min = (v < w) ? v : w;
return u <= max && u >= min;
}
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net