資源簡介
左單擊添加點
左雙擊完成圖形閉合
右雙擊刪除圖形
右單擊撤回一個點
鼠標靠進點可以修改圖形
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Threading.Tasks;
using?System.Windows.Forms;
namespace?DrawPolygon
{
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????????Setstyle(Controlstyles.UserPaint?true);
????????????Setstyle(Controlstyles.AllPaintingInWmPaint?true);?//?禁止擦除背景.??
????????????Setstyle(Controlstyles.DoubleBuffer?true);?//?雙緩沖
????????}
????????List?ListPolyon?=?new?List();
????????List?_tempPolyon?=?new?List();
????????Point?_tempPoint?=?new?Point();
????????bool?IsDrawPolyonstart?=?false;
????????bool?IsEdit?=?false;
????????int?SelectListIndex?=?0;
????????int?SelectRc?=?0;
????????public?float?isLeft1(Point?P0?Point?P1?Point?P2)
????????{
????????????float?abc?=?((P1.X?-?P0.X)?*?(P2.Y?-?P0.Y)?-?(P2.X?-?P0.X)?*?(P1.Y?-?P0.Y));
????????????return?abc;
????????}
????????private?bool?PointInFences(Point?pnt1?Point[]?fencePnts)
????????{
????????????int?wn?=?0?j?=?0;?//wn?計數器?j第二個點指針
????????????for?(int?i?=?0;?i?????????????{//開始循環
????????????????if?(i?==?fencePnts.Length?-?1)
????????????????????j?=?0;//如果?循環到最后一點?第二個指針指向第一點
????????????????else
????????????????????j?=?j?+?1;?//如果不是?,則找下一點
????????????????if?(fencePnts[i].Y?<=?pnt1.Y)?//?如果多邊形的點?小于等于?選定點的?Y?坐標
????????????????{
????????????????????if?(fencePnts[j].Y?>?pnt1.Y)?//?如果多邊形的下一點?大于于?選定點的?Y?坐標
????????????????????{
????????????????????????if?(isLeft1(fencePnts[i]?fencePnts[j]?pnt1)?>?0)
????????????????????????{
????????????????????????????wn++;
????????????????????????}
????????????????????}
????????????????}
????????????????else
????????????????{
????????????????????if?(fencePnts[j].Y?<=?pnt1.Y)
????????????????????{
????????????????????????if?(isLeft1(fencePnts[i]?fencePnts[j]?pnt1)?0)
????????????????????????{
????????????????????????????wn--;
????????????????????????}
????????????????????}
????????????????}
????????????}
????????????if?(wn?==?0)
????????????????return?false;
????????????else
????????????????return?true;
????????}
????????private?void?pictureBox1_MouseDoubleClick(object?sender?MouseEventArgs?e)
????????{
????????????if?(!IsDrawPolyonstart)
????????????{
????????????????if?(_tempPolyon.Count?<=?2)
????????????????{
????????????????????MessageBox.Show(“多邊形至少三個點“);
????????????????????return;
????????????????}
????????????????IsDrawPolyonstart?=?true;
????????????????_tempPolyon.Add(new?Point(e.X?e.Y));
????????????????_tempPolyon.Add(_tempPolyon[0]);
????????????????ListPolyon.Add(_tempPolyon.ToArray());
????????????????_tempPolyon?=?new?List();
????????????}
????????????if?(IsDrawPolyonstart?&&?e.Button?==?MouseButtons.Right)
????????????{
????????????????for?(int?i?=
評論
共有 條評論