?? line.cs
字號:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace DrawLines
{
/// <summary>
/// Line 表示一條線段。
/// </summary>
public class Line
{
#region 構造函數
/// <summary>
/// 構造函數。根據給出的點構造一個Line對象。
/// </summary>
/// <param name="begin">線條的起始點。</param>
/// <param name="end">線條的終止點。</param>
public Line(Point begin, Point end)
{
this.Begin = begin;
this.End = end;
pralDistnc = 5;
}
#endregion
#region 線段的端點
/// <summary>
/// 線段的起始點。
/// </summary>
public Point Begin
{
get
{
return begin;
}
set
{
begin = value;
}
}
private Point begin;
/// <summary>
/// 線段的終止點。
/// </summary>
public Point End
{
get
{
return end;
}
set
{
end = value;
}
}
private Point end;
private int pralDistnc; //平行線的距離。
#endregion
#region 相關數據計算
/// <summary>
/// 返回線段的斜率。
/// </summary>
public float Slope
{
get
{
if (Begin.X == End.X)
return Int32.MaxValue;
return (float)(Begin.Y - End.Y) / (Begin.X - End.X);
}
}
/// <summary>
/// 本線段與線段line是否相交。
/// </summary>
/// <param name="line">檢查是否與本線段相交的線段。</param>
/// <returns>若相交,返回真,否則返回假。</returns>
public bool IsCutWithLine(Line line)
{
if (this.Slope == line.Slope)
return false; //兩條線段平行。
if (this.Begin == this.End || line.Begin == line.End)
return false; //有一條線段是點。
// 計算兩線段所在直線的交點的橫坐標。
float CutPointX = (this.Slope * this.Begin.X - line.Slope * line.Begin.X
- this.Begin.Y + line.Begin.Y) / (this.Slope - line.Slope);
if (CutPointX < this.Begin.X && CutPointX < this.End.X)
return false; //交點在本線段的左側。
if (CutPointX > this.Begin.X && CutPointX > this.End.X)
return false; //交點在本線段的右側。
if (CutPointX < line.Begin.X && CutPointX < line.End.X)
return false; //交點在line的左側。
if (CutPointX > line.Begin.X && CutPointX > line.End.X)
return false; //交點在line的右側。
return true; //交點在本線段和line上,說明兩線段相交。
}
#endregion
#region 繪制線段
/// <summary>
/// 繪制線段以及兩條平行線。
/// </summary>
/// <param name="dc">繪制的圖片對象。</param>
/// <param name="pen">繪制使用的鋼筆。</param>
public void Draw(Graphics dc, Pen pen)
{
//繪制本條線段。
dc.DrawLine(pen, Begin, End);
//繪制兩條平行線。
float tanCida = -1 / this.Slope;
//繪制第一條平行線。
float sinCida = (float)Math.Sin(Math.Atan(tanCida));
float cosCida = sinCida / tanCida;
float x = Begin.X + pralDistnc * cosCida;
float y = Begin.Y + pralDistnc * sinCida;
Point pralBegin = new Point((int)(x + 0.5), (int)(y + 0.5));
x = End.X + pralDistnc * cosCida;
y = End.Y + pralDistnc * sinCida;
Point pralEnd = new Point((int)(x + 0.5), (int)(y + 0.5));
Pen pralPen = new Pen(pen.Color, pen.Width);
pralPen.DashStyle = DashStyle.Dot;
dc.DrawLine(pralPen, pralBegin, pralEnd);
//繪制第二條平行線
sinCida = (float)Math.Sin(Math.Atan(tanCida)) * -1;
cosCida = sinCida / tanCida;
x = Begin.X + pralDistnc * cosCida;
y = Begin.Y + pralDistnc * sinCida;
pralBegin = new Point((int)(x + 0.5), (int)(y + 0.5));
x = End.X + pralDistnc * cosCida;
y = End.Y + pralDistnc * sinCida;
pralEnd = new Point((int)(x + 0.5), (int)(y + 0.5));
dc.DrawLine(pralPen, pralBegin, pralEnd);
}
#endregion
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -