1、已知一线段 AB,在某个方向选择一点 P,输入距离 d,求线段 AB 延长距离 d 后的点坐
标;
protected PointF GetAppendDist(PointF temp,List
pts)
{
PointF temppt;
PointF pt = temp;
PointF fpt = new PointF(pts[0].X, pts[0].Y);
PointF lpt = new PointF(pts[1].X, pts[1].Y);
Vector3D Linevec = new Vector3D(lines[1].X - lines[0].X, lines[1].Y - lines[0].Y, 0);
double Len = Linevec.Length;
Vector3D linepa = new Vector3D(pt.X - fpt.X, pt.Y - fpt.Y, 0);
double pa = linepa.Length;
Vector3D linepb = new Vector3D(pt.X - lpt.X, pt.Y - lpt.Y, 0);
double pb = linepb.Length;
double distance = Convert.ToDouble(this.appenddist.Text.ToString());
float xd, yd;
if (pa > pb)
{
xd = lpt.X + ((lpt.X - fpt.X) * (float)distance) / (float)Len;
yd = lpt.Y + ((lpt.Y - fpt.Y) * (float)distance) / (float)Len;
temppt = new PointF(xd, yd);
}
else
{
xd = fpt.X + ((fpt.X - lpt.X) * (float)distance) / (float)Len;
yd = fpt.Y + ((fpt.Y - lpt.Y) * (float)distance) / (float)Len;
temppt = new PointF(xd, yd);
}
return temppt;
}
2、判断两线段是否相交;
public float maxvalue(float p1,float p2)
{
float p=0;
if (p1 >= p2)
{
p = p1;
}
else p = p2;
return p;
}
public float minvalue(float p1, float p2)
{
float p = 0;
if (p1 <= p2)
{
p = p1;
}
else p = p2;
return p;
}
protected void isLineInterset(List
lines)
{
float maxPx = maxvalue(lines[0].X, lines[1].X);//线段 p 中最大 X 坐标
float maxQx = maxvalue(lines[2].X, lines[3].X); //线段 q 中最大 X 坐标
float maxPy = maxvalue(lines[0].Y, lines[1].Y);//线段 p 中最大 Y 坐标
float maxQy = maxvalue(lines[2].Y, lines[3].Y);//线段 q 中最大 Y 坐标
float minPx = minvalue(lines[0].X, lines[1].X);//线段 p 中最小 X 坐标
float minQx = minvalue(lines[2].X, lines[3].X);//线段 q 中最小 X 坐标
float minPy = minvalue(lines[0].Y, lines[1].Y);//线段 p 中最小 Y 坐标
float minQy = minvalue(lines[2].Y, lines[3].Y);//线段 q 中最小 Y 坐标
if (maxPx <= minQx || maxQx <= minPx || maxPy <= minQy || maxQy <=
minPy)
{
MessageBox.Show("两线段不相交...");
return;
}
else
{
Vector3D VecP1 = new Vector3D(lines[0].X, lines[0].Y, 0);
Vector3D VecP2 = new Vector3D(lines[1].X, lines[1].Y, 0);
Vector3D VecQ1 = new Vector3D(lines[2].X, lines[2].Y, 0);
Vector3D VecQ2 = new Vector3D(lines[3].X, lines[3].Y, 0);
Vector3D CrossProduct1 = Vector3D.CrossProduct((VecP1 - VecQ1),
(VecQ2 - VecQ1));
Vector3D CrossProduct2 = Vector3D.CrossProduct((VecQ2 - VecQ1),
(VecP2 - VecQ1));
Vector3D CrossProduct3 = Vector3D.CrossProduct((VecQ1 - VecP1),
(VecP2 - VecQ1));
Vector3D CrossProduct4 = Vector3D.CrossProduct((VecP2 - VecP1),
(VecQ2 - VecP1));
if
(Vector3D.DotProduct(CrossProduct1, CrossProduct2) >= 0 &&
Vector3D.DotProduct(CrossProduct3, CrossProduct4) >= 0)
{
MessageBox.Show("两直线相交");
}
else MessageBox.Show("两直线不相交!");
}
}
3、多边形重心计算;
int n=poly .Count ;
float[] PolyCentroid;
float[] Xpoints = new float[n];
float[] Ypoints = new float[n];
for (int i = 0; i <= poly.Count - 1;i++ )
{
Xpoints[i] = poly[i].X;
Ypoints[i] = poly[i].Y;
}
PolyCentroid = polyCentroid(Xpoints,Ypoints, n);
protected float[] polyCentroid(float[] x, float[] y, int n)
{
float[] Centroid = null;
float XCentroid, YCentroid, area;
int i, j;
float ai, atmp = 0, xtmp = 0, ytmp = 0;
for (i = n - 1, j = 0; j < n;i=j,j++ )
{
ai = x[i] * y[j] - x[j] * y[i];
atmp += ai;
xtmp += (x[j] + x[i]) * ai;
ytmp += (y[j] + y[i]) * ai;
}
area = atmp / 2;
if (atmp!=0)
{
XCentroid = xtmp / (3 * atmp);
YCentroid = ytmp / (3 * atmp);
}
else { return null; }
Centroid = new float[] { XCentroid, YCentroid };
return Centroid;
}
4、编程实现八方向矢量线栅格化;
protected void RasterLineByCenter(PointF fpt,PointF lpt)//八方向栅格化
{
PointF p1 = fpt; PointF p2 = lpt; PointF Center=new PointF();
int Size=10;
int k = (int)(lines[0].Y / 10); int m = (int)(lines[0].X / 10);
int k1 = (int)(lines[1].Y / 10); int m1 = (int)(lines[1].X / 10);
if (Math.Abs(lines[1].Y - lines[0].Y) >= Math.Abs(lines[1].X - lines[0].X))//行数大于
列数
{
if ((p2.Y - p1.Y) != 0)
{
int tempRow, tempRow1;
if (k < k1)
{
tempRow = k;
tempRow1 = k1;
}
else
{
tempRow = k1;
tempRow1 = k;
}
float pk = (p2.X - p1.X) / (p2.Y - p1.Y);
for (int Row = tempRow; Row <= tempRow1; Row++)
{
float CenterY = ((Row + 1) * Size + Row * Size) / 2;
float CenterX = (CenterY - p1.Y) * pk + p1.X;
Center = new PointF(CenterX, CenterY);
int i = (int)CenterY / 10;
int j = (int)CenterX / 10;
RasterationLine(db[i, j]);
ptsnew.Add(new PointF(i+1, j+1));
}
}
else //水平线栅格化
{
if (m > m1) //首点在右,尾点在左
{
for (int clo = m1 + 1; clo <= m; clo++)
{
RasterationLine(db[k, clo]);
ptsnew.Add(new PointF(k+1, clo+1));
}
}
else //首点在左,尾点在右
{
for (int clo = m + 1; clo <= m1; clo++)
{
RasterationLine(db[k, clo]);
ptsnew.Add(new PointF(k+1, clo+1));
}
}
}
}
else //列数大于行数的情况
{
if ((p2.X - p1.X) != 0)
{
int tempCol, tempCol1;
if (m < m1)
{
tempCol = m;
tempCol1 = m1;
}
else
{
tempCol = m1;
tempCol1 = m;
}
float pk=(p2.Y-p1.Y)/(p2.X-p1.X);
for (int Col = tempCol; Col <= tempCol1; Col++)
{
float CenterX = ((Col + 1) * Size+Col*Size) / 2;
float CenterY = (CenterX - p1.X) * pk + p1.Y;
Center = new PointF(CenterX, CenterY);
int i = (int)CenterY / 10;
int j = (int)CenterX / 10;
RasterationLine(db[i, j]);
ptsnew.Add(new PointF(i+1, j+1));
}
}
else //垂直线栅格化
{
if (k > k1) //首点在下,尾点在上
{
for (int row = k1; row <= k; row++)
{
RasterationLine(db[row, m]);
ptsnew.Add(new PointF(row+1, m+1));
}
}
else //首点在上,尾点在下
{
for (int row = k; row <= k1; row++)
{
RasterationLine(db[row, m]);
ptsnew.Add(new PointF(row+1, m+1));
}
}
}
}
}
5、编程实现全路径矢量线栅格化;
protected void RasterLineByAllRoad(PointF fpt,PointF lpt)//全路径栅格化
{
float Xabs = Math.Abs(lpt.X - fpt.X);
float Yabs = Math.Abs(lpt.Y - fpt.Y);
int i1 = (int)(fpt.Y / 10); int j1=(int)(fpt.X/10);
int i2 = (int)(lpt.Y / 10); int j2=(int)(lpt.X/10);
int m=10;//网格大小
int y0=this.pictureBox1.Height;
int Row = (int)y0 / 10;
int x0=this.pictureBox1.Width;
int Col = (int)x0 / 10;
if (Xabs >= Yabs) //列数大于行数
{
int sign = 0;
if(Xabs !=0)
{
float tan = (lpt.Y - fpt.Y) / (lpt.X - fpt.X);
if (lpt.Y>fpt.Y) //i1 fpt.X) //j1= j2) je = j2;
Rasterfromto(sign,i, ja, je);
ja = je;
}
}
else //j1>j2
{
for (int i = i1; i <= i2;i++ )
{
int je = Convert.ToInt32((((i + 1) * m - fpt.Y) / tan + fpt.X) /
m);
if (je <= j2) je = j2;
Rasterfromto(sign, i, ja, je);
ja = je;
}
}
}
else //i1>i2
{
int ja = j2;
if (lpt.X > fpt.X) //j1j2
{
for (int i = i2; i <= i1; i++)
{
int je = Convert.ToInt32((((i + 1) * m - lpt.Y) / tan + lpt.X) /
m);
if (je >= j1) je = j1;
Rasterfromto(sign, i, ja, je);
ja = je;
}
}
}
}
else //水平线 i1=i2
{
Rasterfromto(sign, i1, j1, j2);
}
}
else //行数大于列数
{
int sign = 1;
if (Yabs != 0)
{
float tan = (lpt.X - fpt.X) / (lpt.Y - fpt.Y);
if (lpt.X < fpt.X) //j1>j2
{
int ia = i2;
if (lpt.Y > fpt.Y) //i1i2
{
for (int j = j2; j <= j1; j++)
{
int ie = Convert.ToInt32((((j + 1) * m - lpt.X) / tan + lpt.Y) /
m);
if (ie >= i1) ie = i1;
Rasterfromto(sign, j, ia, ie);
ia = ie;
}
}
}
else //j1 fpt.Y) //i1= i2) ie = i2;