cloudflight
2023-12-02 1a5ef6b2bf25de50b685b44299d9a049a2feac80
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using static System.Windows.Forms.AxHost;
using static System.Windows.Forms.LinkLabel;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrackBar;
 
namespace CloudWaterNetwork
{
    public partial class PumpViewer : UserControl
    {
        private List<Node> _junctions=new List<Node>();
        private List<Link> _links=new List<Link>();
        private List<Area> _areas=new List<Area>();
 
        private Bitmap buffer;
        private Point offset=new Point(0,0);
        private float zoom = 1.0f;
        private double Rotation = 0;
        private double Rotation0 = 0;
        private StatusStrip statusStrip1;
        private ToolStripStatusLabel label_center;
        private ToolStripStatusLabel label_zoom;
        private ToolStripStatusLabel label_mouse;
        private PointF MapCenter;
        private PointF MapCenter0;
        private Label label_choose;
        private bool is3Dview = false;
        
        public List<Node> junctions
        {
            get { return _junctions; }
            set { _junctions = value; }
        }
 
        public List<Link> Pipes
        {
            get { return _links; }
            set { _links = value; }
        }
 
        public List<Area> Areas
        {
            get { return _areas; }
            set { _areas = value; }
        }
 
        //public float Scale
        //{
        //    get { return zoom; }
        //    set { zoom = value; }
        //}
 
        public Point PanningOffset
        {
            get { return offset; }
            set { offset = value; }
        }
 
        public PumpViewer()
        {
            InitializeComponent();
            junctions = new List<Node>();
            Pipes = new List<Link>();
            Areas = new List<Area>();
            MapCenter = PointF.Empty;
            zoom = 0.1f;
            DoubleBuffered = true;
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            BackColor = Color.Transparent;
        }
 
        public void SetData(Network network)
        {
            SetData(network.Nodes, network.Links, network.areas);
            
        }
        public void Set3DView(bool is3Dview)
        {
            this.is3Dview = is3Dview;
            Invalidate();
        }
        public void SetData(List<Node> Junctions, List<Link> Links, List<Area> Areas)
        {
            this.junctions = Junctions;
            this.Pipes = Links;
            this.Areas = Areas;
            MapCenter = PointF.Empty;
            float x0=99999999999f, y0= 99999999999f, x1=-99999999999f, y1=-99999999999f;
 
            foreach (Node junction in Junctions)
            {
                MapCenter.X +=(float) junction.X;
                MapCenter.Y +=(float) junction.Y;
                if (x0>junction.X) x0=junction.X;
                if (y0>junction.Y) y0=junction.Y;
                if (x1<junction.X) x1=junction.X;
                if (y1<junction.Y) y1=junction.Y;
            }
 
            float x_span = x1 - x0,y_span = y1 - y0;
 
            zoom = Math.Max(Width/ x_span,  Height/ y_span);
 
 
            
 
            if (Junctions.Count > 0)
            {
                MapCenter.X /= Junctions.Count;
                MapCenter.Y /= Junctions.Count;
            }
 
            Invalidate();
        }
 
        public void SetRotation(double d)
        {
            this.Rotation = d;
            Invalidate();
        }
 
    
        /// <summary>
        /// 将屏幕坐标转换为世界坐标。输入屏幕坐标 (x,y),返回世界坐标 (wx, wy)。
        /// </summary>
        /// <param name="screenPos"></param>
        /// <returns></returns>
        public PointF ScreenToMap(PointF screenPos,float z=0)
        {
            
            var centerX = Width / 2;
            var centerY = Height / 2;
            var worldX = (screenPos.X  - centerX - Z(z).X) / Zoom.X + MapCenter.X;
            var worldY = (screenPos.Y  - centerY - Z(z).Y) / Zoom.Y + MapCenter.Y;
            //if (is3Dview) worldY = -(screenPos.Y - centerY + 2 * z) / (0.5f* zoom) + center.Y;
            return new PointF(worldX, worldY);
        }
      
        public PointF MapToScreen(PointF mapPos, float z=0)
        {
            
            var centerX = Width  / 2;
            var centerY = Height / 2;
            var screenX = (mapPos.X - MapCenter.X) * Zoom.X + centerX + Z(z).X;
            var screenY = (mapPos.Y - MapCenter.Y) * Zoom.Y + centerY + Z(z).Y;
            //if (is3Dview) screenY = -(mapPos.Y - center.Y) * (0.5f * zoom) + centerY - 2 * z;
            return new PointF(screenX, screenY);
        }
 
        public float Get_dist(PointF A, PointF B)
        {
            float dx = A.X - B.X;
            float dy = A.Y - B.Y;
            float dist = (float)Math.Sqrt(dx * dx + dy * dy);
            return dist;
        }
 
        PointF PMin_Show, PMax_Show;
 
        /// <summary>
        /// 判断是否在屏幕坐标内
        /// </summary>
        /// <param name="screenPos"></param>
        /// <returns></returns>
        public bool isVisible( PointF MapPos)
        {
            if (MapPos.X < PMin_Show.X || MapPos.X > PMax_Show.X || MapPos.Y < PMin_Show.Y || MapPos.Y > PMax_Show.Y)
                return false;
            else
                return true;
        }
        ///// <summary>
        ///// 获取屏幕距离
        ///// </summary>
        //private double GetDistOnScreen(double dist)
        //{
        //}
        //Graphics bufferG=null;
 
 
        // 根据旋转角度计算旋转后的坐标
        private PointF GetRotatePoint(PointF p )
        {
            PointF center = MapCenter;
            double radian = Rotation * Math.PI / 180;  // 角度转弧度
            float x = (float)(Math.Cos(radian) * (p.X - center.X) - Math.Sin(radian) * (p.Y - center.Y) + center.X);
            float y = (float)(Math.Sin(radian) * (p.X - center.X) + Math.Cos(radian) * (p.Y - center.Y) + center.Y);
            return new PointF(x, y);
        }
        private PointF GetRotateVector(PointF p,PointF p0)
        {
         
            double radian = Rotation * Math.PI / 180;  // 角度转弧度
            float x = (float)(Math.Cos(radian) * (p.X - p0.X)  - Math.Sin(radian) * (p.Y - p0.Y));
            float y = (float)(Math.Sin(radian) * (p.X - p0.X)  + Math.Cos(radian) * (p.Y - p0.Y));
            return new PointF(x, y);
        }
        private PointF TransformPoint(PointF point, float z = 0)
        {
            var pointR = GetRotatePoint(point);
            var n=new PointF((float)pointR.X - Z(z).X, (float)(pointR.Y - Z(z).Y));
            return n;
        }
        private PointF GetMapPoint_3D(Node junction)
        {
            PointF p;
         
            p = TransformPoint(junction.Position, junction.Elev);
            return p;
        
        }
        
        //private Point3D pointoff()
        //{
 
        //}
 
        public PointF Zoom
        {
            get
            {
                var p = new PointF(0, 0);
                p.X = zoom;
                p.Y = is3Dview ?-0.5f * zoom : -1f * zoom;
                return p;
            }           
        }
 
 
        private PointF Z(float z)
        {
            if (is3Dview)
            {
                var point = new PointF(0, -2f * z);
                return point;
            }
            else
            {
                return new PointF(0, 0);
            }
            
        }
       
 
 
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            
            if (buffer == null || buffer.Width != Width || buffer.Height != Height)
            {
                buffer?.Dispose();
                buffer = new Bitmap(Width, Height);
            }
            // 使用缓存绘制,避免在每次重绘时重新计算所有要绘制的元素
 
            //if (bufferG == null) bufferG = Graphics.FromImage(buffer);
            using (var bufferG = Graphics.FromImage(buffer))
            //using (var bufferG = e.Graphics)
            {
                // 先将控件的背景填充为白色
                
                bufferG.Clear(Color.Transparent);
                //bufferG.TranslateTransform(Width/2, Height/2);
                //bufferG.ScaleTransform(Zoom.X, Zoom.Y);
                //if (!this.is3Dview)
                //{
                //    bufferG.ScaleTransform(zoom, -zoom);
                //}
 
                //else
                //{
                //    //bufferG.RotateTransform(30);
                //    bufferG.ScaleTransform(zoom, (float)-0.5* zoom);
                //}
 
                //bufferG.TranslateTransform(-MapCenter.X, -MapCenter.Y);
 
 
 
                //List<PointF> diametersZoom = new List<PointF>() { new PointF(0, 0.8f), new PointF(150, 0.3f), new PointF(300, 0.001f), new PointF(800, 0.0001f) };
 
                //using (SolidBrush brush = new SolidBrush(Color.FromArgb(0, 255, 0, 0)))
                //{
                //    foreach (Area area in _areas)
                //    {
                //        bufferG.FillPolygon(brush, area.Points.Select(
                //            p => new PointF((float) (p.X), (float)(p.Y))
                //        ).ToArray());
                //    }
                //}
                //Pen penChoosed = new Pen(Color.Purple, 5);
                //// 绘制线
 
                //using (Pen pen = new Pen(Color.FromArgb(0, 0, 255), 2))
                //{
                //    foreach (var link in _links)
                //    {
                //        if (!isVisible(link.Position)) continue;
                //        float zoomAtMin = 0;
                //        for (int i = 0; i < diametersZoom.Count; i++)
                //        {
                //            PointF point = diametersZoom[i];
                //            if (link.Diameter >= point.X) continue;
                //            zoomAtMin = diametersZoom[i - 1].Y;
                //            break;
                //        }
                //        if (zoomAtMin >= zoom) continue;
 
 
                //        bufferG.DrawLines(link.Choosed?penChoosed: pen, new PointF[] {GetMapPoint_3D(link.StartPoint),GetMapPoint_3D(link.EndPoint) });
 
                //    } 
                //}
 
                //SolidBrush brushChoosed = new SolidBrush(Color.Green);
 
                //using (SolidBrush brush = new SolidBrush(Color.FromArgb(255, 0, 0)))
                //{
                //    foreach (Junction junction in _junctions)
                //    {
                //        if (!isVisible( junction.Position)) continue;
                //        var radius = 4.0f * zoom;
                //        //var x = junction.Position.X * zoom + PanningOffset.X - radius / 2.0f;
                //        //var y = junction.Position.Y * zoom + PanningOffset.Y - radius / 2.0f;
 
                //        PointF p = GetMapPoint_3D(junction);
 
 
 
                //        var rectangle = new RectangleF((float)p.X - 5, (float)p.Y - 5, 10, 10);
                //        float zoomAtMin = 0;
                //        for (int i = 0; i < diametersZoom.Count; i++)
                //        {
                //            PointF point = diametersZoom[i];
                //            if (junction.MaxDiameter >= point.X) continue;
                //            zoomAtMin = diametersZoom[i - 1].Y;
                //            break;
                //        }
                //        if (zoomAtMin >= zoom) continue;
                //        bufferG.FillEllipse(junction.Choosed ? brushChoosed :  brush, rectangle);
                //    }
                //}
                base.OnPaint(e);
                if (this.BackgroundImage != null)
                {
                    float ratio = (float)this.BackgroundImage.Width / this.BackgroundImage.Height;
                    RectangleF destRect = new RectangleF(0, 0, this.Width, this.Width / ratio * 2 / 3);
                    e.Graphics.DrawImage(this.BackgroundImage, destRect);
                }
 
                if (isDragging)
                {
                    var _lastMousePosition = DragStartPos;
                    
                    // 绘制矩形
                    var start = new Point((int)Math.Min(mousePosition.X, _lastMousePosition.X), (int)Math.Min(mousePosition.Y, _lastMousePosition.Y));
                    var size = new Size((int)Math.Abs(_lastMousePosition.X - mousePosition.X), (int)Math.Abs(_lastMousePosition.Y - mousePosition.Y));
                    var rectangle0 = new Rectangle(start, size);
                    using (var pen = new Pen(Color.Black, 2))
                    {
                        bufferG.DrawRectangle(pen, rectangle0);
                    }
                    
                }
                
            }
 
            // 将生成的画布绘制到控件上
            e.Graphics.DrawImage(buffer, 0,0);
            //BackColor = Color.FromArgb(500, BackColor);
        }
        public new void Invalidate()
        {
            base.Invalidate();
            PMin_Show = ScreenToMap(new PointF(0, Height));
            PMax_Show = ScreenToMap(new PointF(Width, 0));
        }
       
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
 
            // 当控件尺寸改变时,触发重绘
            this.Invalidate();
            
        }
        public PointF mouseXY=new PointF(0,0);
 
        PointF DragStartPos;
        PointF RotaStartPos;
        bool isPanning;
        bool isDragging;
        bool isRotating;
        List<MapObject> ChoosedObjs = new List<MapObject>();
        PointF mousePosition;
        // control+鼠标中间按下缩放
 
 
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
             if (e.Button == MouseButtons.Middle )
            {
                
                if (ModifierKeys == Keys.Control)
                {
                    DragStartPos = ScreenToMap( new PointF(e.X, e.Y));
                    
                    isDragging = true;
                }
                else
                {
                    this.Cursor = Cursors.SizeAll;
                    MapCenter0 = MapCenter;
                    isPanning = true;
                }
                
            }
            else if (e.Button==MouseButtons.Left)
            {
                DragStartPos = ScreenToMap(new PointF(e.X, e.Y));
                isDragging = true;
            }
            else if (e.Button==MouseButtons.Right)
            {
                RotaStartPos = new PointF(e.X, e.Y);
                Rotation0 = Rotation;
                isRotating = true;
            }
            
        }
        bool controlDown = false;
     
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
 
            var MP = ScreenToMap(new PointF(e.X, e.Y));
            if (isPanning)
            {
                var vector = GetRotateVector(new PointF(e.X,e.Y), new PointF(_lastMouseX, _lastMouseY));
                //center.X -= e.X / zoom - _lastMouseX / zoom;
                //center.Y += e.Y / zoom - _lastMouseY / zoom;
 
 
 
                //MapCenter.X -= (e.X - _lastMouseX) / Zoom.X;
                //MapCenter.Y -= (e.Y - _lastMouseY) / Zoom.Y;
                MapCenter.X -=  vector.X / Zoom.X ;
                MapCenter.Y -=  vector.Y / Zoom.Y ;
 
 
                label_center.Text = $"center:({MapCenter.X.ToString("0.000")} ,{MapCenter.Y.ToString("0.000")})";
                Invalidate();
            }
            else if (isDragging)
            {
                mousePosition = MP;
                // 绘制选择框                                                                     
                // Rectangle selectionRect = new Rectangle(
                //    Math.Min((int)DragStartPos.X, (int)MP.X),
                //    Math.Min((int)DragStartPos.Y, (int)MP.Y),
                //    Math.Abs((int)DragStartPos.X - (int)MP.X),
                //    Math.Abs((int)DragStartPos.Y - (int)MP.Y));
                //DrawSelectionRect(selectionRect);
                Invalidate();
            }
            else if (isRotating)
            {
                mousePosition = MP;
                Rotation= Rotation0 + ((float)e.X- (float)RotaStartPos.X)  * 180 *2.5 / (float)this.Width;
                Invalidate();
            }
 
 
 
            label_mouse.Text = $"X:{e.X.ToString("0.000")} Y:{e.Y.ToString("0.000")} [Map]X:{MP.X.ToString("0.000")} Y:{MP.Y.ToString("0.000")}";
            _lastMouseX = e.X;
            _lastMouseY = e.Y;
        }
 
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
 
            if (e.Button == MouseButtons.Middle)
            {
                if (ModifierKeys == Keys.Control)
                {
                    isDragging = false;
                    Cursor = Cursors.Default;
 
                    var _lastMousePosition = DragStartPos;
 
                    // 绘制矩形
                    var start = new Point((int)Math.Min(mousePosition.X, _lastMousePosition.X), (int)Math.Min(mousePosition.Y, _lastMousePosition.Y));
                    var size = new Size((int)Math.Abs(_lastMousePosition.X - mousePosition.X), (int)Math.Abs(_lastMousePosition.Y - mousePosition.Y));
                    var rectangle0 = new Rectangle(start, size);
                    ChoosedObjs.ForEach(obj => obj.Selected = false);
                    ChoosedObjs.Clear();
                    for (int i = 0; i < junctions.Count; i++)
                    {
                        PointF p = GetMapPoint_3D(junctions[i]);
                        if (rectangle0.Contains(p.ToPoint()))
                        {                           
                            junctions[i].Selected = true;
                            ChoosedObjs.Add(junctions[i]);                          
                        }
                    }
                   
                    for (int i = 0; i < Pipes.Count; i++)
                    {
                        //PointF p = GetPointF(Pipes[i]);
                        if (rectangle0.Contains(Pipes[i].Position.ToPoint()))
                        {
                            Pipes[i].Selected = true;
                            ChoosedObjs.Add(Pipes[i]);                           
                        }
                    }
                    Invalidate();
 
                    //zoom *= (float)Math.Pow(2, e.Delta / 120.0 / 10.0);
                    //zoom = Math.Max(0.1f, Math.Min(10.0f, zoom));
 
                    //center.X += e.Location.X / oldZoom - e.Location.X / zoom;
                    //center.Y += e.Location.Y / oldZoom - e.Location.Y / zoom;
                    label_center.Text = $"center:({MapCenter.X.ToString("0.000")} ,{MapCenter.Y.ToString("0.000")})";
                    label_zoom.Text = $"Zoom:{zoom.ToString("0.000")}";
                    Invalidate();
                }
                else
                {
                    Cursor = Cursors.Default;
                    isPanning = false;
                }
                   
            }else if (e.Button==MouseButtons.Left)
            {
                
 
 
                if (mousePosition != new PointF(0, 0))
                {
 
                    isDragging = false;
                    Cursor = Cursors.Default;
                    var _lastMousePosition = DragStartPos;
 
                    // 绘制矩形
                    var start = new Point((int)Math.Min(mousePosition.X, _lastMousePosition.X), (int)Math.Min(mousePosition.Y, _lastMousePosition.Y));
                    var size = new Size((int)Math.Abs(_lastMousePosition.X - mousePosition.X), (int)Math.Abs(_lastMousePosition.Y - mousePosition.Y));
                    var rectangle0 = new Rectangle(start, size);
                    ChoosedObjs.ForEach(obj => obj.Selected = false);
                    ChoosedObjs.Clear();
                    for (int i = 0; i < junctions.Count; i++)
                    {
 
                        PointF p = GetMapPoint_3D(junctions[i]);
                        if (rectangle0.Contains(p.ToPoint()))
                        {
                            junctions[i].Selected = true;
                            ChoosedObjs.Add(junctions[i]);
                        }
                    }
 
                    for (int i = 0; i < Pipes.Count; i++)
                    {
                        if (rectangle0.Contains(Pipes[i].Position.ToPoint()))
                        {
                            Pipes[i].Selected = true;
                            ChoosedObjs.Add(Pipes[i]);
                        }
                    }
                    GlobalObject.PropertyForm.SetObjs(ChoosedObjs); 
                    Invalidate();
                    mousePosition = new PointF(0, 0);
                }
                else
                {
                    isDragging = false;
 
                    // 遍历所有点,找出最近的点
                    PointF clickedPoint = new PointF(e.X, e.Y);  //ScreenToMap(new PointF(e.X, e.Y));
                    float minDist = float.MaxValue;
                    float DistLimit = 5f;
                    int minIndex = -1;
                    bool isJunction = true;
                    
                    for (int i = 0; i < junctions.Count; i++)
                    {
                        PointF mapPos = GetRotatePoint(junctions[i].Position);
                        PointF currentPoint =MapToScreen(mapPos, junctions[i].Elev);
                        float dist = Get_dist(clickedPoint, currentPoint );
                        if (dist < minDist)
                        {
                            minDist = dist;
                            minIndex = i;
                        }
                    }
                    for (int i = 0; i < Pipes.Count; i++)
                    {
                        //float dist = (clickedPoint.X - Pipes[i].X) * (clickedPoint.X - Pipes[i].X) +
                        //    (clickedPoint.Y - Pipes[i].Y) * (clickedPoint.Y - Pipes[i].Y);
                        PointF mapPos = GetRotatePoint(Pipes[i].Position);
                        PointF currentPoint = MapToScreen(mapPos, Pipes[i].Elev);
                        float dist = Get_dist(clickedPoint, currentPoint);
                        if (dist < minDist)
                        {
                            minDist = dist;
                            minIndex = i;
                            isJunction = false;
                        }
                    }
                    if (minIndex >= 0 && minDist<=DistLimit)
                    {
                        if (isJunction)
                        {
                            ChoosedObjs.ForEach(obj => obj.Selected = false);
                            ChoosedObjs.Clear();
                            junctions[minIndex].Selected = true;
                            ChoosedObjs.Add(junctions[minIndex]);
                            Invalidate();
                            // 显示该点的属性
                            ShowProperties(junctions[minIndex]);
                        }
                        else
                        {
                            ChoosedObjs.ForEach(obj => obj.Selected = false);
                            ChoosedObjs.Clear();
                            Pipes[minIndex].Selected = true;
                            ChoosedObjs.Add(Pipes[minIndex]);
                            Invalidate();
                            // 显示该点的属性
                            ShowProperties(Pipes[minIndex]);
                        }
                    }
                    else
                    {
                        ChoosedObjs.ForEach(obj => obj.Selected = false);
                        ChoosedObjs.Clear();
                        Invalidate();
                    }
                }
 
 
 
 
 
 
                //zoom *= (float)Math.Pow(2, e.Delta / 120.0 / 10.0);
                //zoom = Math.Max(0.1f, Math.Min(10.0f, zoom));
 
                //center.X += e.Location.X / oldZoom - e.Location.X / zoom;
                //center.Y += e.Location.Y / oldZoom - e.Location.Y / zoom;
                label_center.Text = $"center:({MapCenter.X.ToString("0.000")} ,{MapCenter.Y.ToString("0.000")})";
                label_zoom.Text = $"Zoom:{zoom.ToString("0.000")}";
                Invalidate();
            }
            else if(e.Button==MouseButtons.Right)
            {
                if (isRotating)
                {
                    isRotating = false;
                }
            }
 
 
            
        }
      
        protected override void OnMouseWheel(MouseEventArgs e)
        {
            base.OnMouseWheel(e);
            float oldZoom = zoom;
            zoom *= (float)Math.Pow(2, e.Delta / 120.0 / 10.0);
            zoom = Math.Max(0.1f, Math.Min(10.0f, zoom));
 
            //center.X += e.Location.X / oldZoom - e.Location.X / zoom;
            //center.Y += e.Location.Y / oldZoom - e.Location.Y / zoom;
            label_center.Text = $"center:({MapCenter.X.ToString("0.000")} ,{MapCenter.Y.ToString("0.000")})";
            label_zoom.Text = $"Zoom:{zoom.ToString("0.000")}";
            Invalidate();
        }
         
        private int _lastMouseX;
        private int _lastMouseY;
 
        // 显示点属性
        private void ShowProperties(MapObject obj)
        {
            string str = "point";
            // TODO: 根据需要实现
            if (obj is Link) str = "pipe";
            //MessageBox.Show($"{str}:({obj.X.ToString("0.000")} ,{obj.Y.ToString("0.000")})");
            label_choose.Text = $"{str}[{obj.ID}]({obj.X.ToString("0.000")},{obj.Y.ToString("0.000")})";
            GlobalObject.PropertyForm.SetObj(obj);
        }
 
        //public void DrawLine(PointF start, PointF end)
        //{
        //    var pipeline = new List<PointF>();
        //    pipeline.Add(start);
        //    pipeline.Add(end);
        //    pipeline.ForEach(pt => pt.Transform(x => (x - _viewCenter) / _scale));
        //    var nearbyPipes = GetNearbyPipes(pipeline);
        //    // TODO: Display nearby pipes
        //}
 
        //private List<Tuple<PointF, PointF>> GetNearbyPipes(List<PointF> pipeline)
        //{
        //    // TODO: Implement this method to retrieve nearby pipes based on the given pipeline
        //    // This method should return a list of Tuple<PointF, PointF>, where each tuple represents a pipe
        //    throw new NotImplementedException();
        //}
 
        private void InitializeComponent()
        {
            this.statusStrip1 = new System.Windows.Forms.StatusStrip();
            this.label_center = new System.Windows.Forms.ToolStripStatusLabel();
            this.label_zoom = new System.Windows.Forms.ToolStripStatusLabel();
            this.label_mouse = new System.Windows.Forms.ToolStripStatusLabel();
            this.label_choose = new System.Windows.Forms.Label();
            this.statusStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // statusStrip1
            // 
            this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.label_center,
            this.label_zoom,
            this.label_mouse});
            this.statusStrip1.Location = new System.Drawing.Point(0, 578);
            this.statusStrip1.Name = "statusStrip1";
            this.statusStrip1.Size = new System.Drawing.Size(719, 22);
            this.statusStrip1.TabIndex = 0;
            this.statusStrip1.Text = "statusStrip1";
            // 
            // label_center
            // 
            this.label_center.Name = "label_center";
            this.label_center.Size = new System.Drawing.Size(73, 17);
            this.label_center.Text = "center:0,0";
            // 
            // label_zoom
            // 
            this.label_zoom.Name = "label_zoom";
            this.label_zoom.Size = new System.Drawing.Size(61, 17);
            this.label_zoom.Text = "Zoom:1";
            // 
            // label_mouse
            // 
            this.label_mouse.Name = "label_mouse";
            this.label_mouse.Size = new System.Drawing.Size(65, 17);
            this.label_mouse.Text = "X:0 Y:0";
            // 
            // label_choose
            // 
            this.label_choose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.label_choose.AutoSize = true;
            this.label_choose.Location = new System.Drawing.Point(444, 584);
            this.label_choose.Name = "label_choose";
            this.label_choose.Size = new System.Drawing.Size(0, 12);
            this.label_choose.TabIndex = 1;
            // 
            // MapViewer
            // 
            this.Controls.Add(this.label_choose);
            this.Controls.Add(this.statusStrip1);
            this.Name = "MapViewer";
            this.Size = new System.Drawing.Size(719, 600);
            this.statusStrip1.ResumeLayout(false);
            this.statusStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();
 
        }
    }
 
}