Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Charting/Primitives/Line.cs @ 483

Last change on this file since 483 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 2.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Drawing;
26using System.Drawing.Drawing2D;
27using System.Windows.Forms;
28
29namespace HeuristicLab.Charting {
30  public class Line : LinearPrimitiveBase {
31    public Line(IChart chart, PointD start, PointD end)
32      : base(chart, start, end) {
33    }
34    public Line(IChart chart, double x1, double y1, double x2, double y2)
35      : this(chart, new PointD(x1, y1), new PointD(x2, y2)) {
36    }
37    public Line(IChart chart, PointD start, PointD end, Pen pen)
38      : base(chart, start, end, pen, null) {
39    }
40    public Line(IChart chart, double x1, double y1, double x2, double y2, Pen pen)
41      : this(chart, new PointD(x1, y1), new PointD(x2, y2), pen) {
42    }
43
44    public override bool ContainsPoint(PointD point) {
45      if (base.ContainsPoint(point)) return true;
46
47      double penWidthX = Chart.PixelToWorldRatio.Width * (Pen.Width + 1);
48      double penWidthY = Chart.PixelToWorldRatio.Height * (Pen.Width + 1);
49
50      if ((point.X < (Math.Min(Start.X, End.X) - (penWidthX / 2))) ||
51          (point.Y < (Math.Min(Start.Y, End.Y) - (penWidthY / 2))) ||
52          (point.X > (Math.Max(Start.X, End.X) + (penWidthX / 2))) ||
53          (point.Y > (Math.Max(Start.Y, End.Y) + (penWidthY / 2)))) {
54        return false;
55      }
56
57      // calculate distance between point P(X,Y) and line
58      // d(P,g) = |AP.n|/|n|
59      Offset start_end = End - Start;
60      Offset n = new Offset(start_end.DY, -1 * start_end.DX);
61      Offset start_point = point - Start;
62      double d = Math.Abs(start_point.DX * n.DX + start_point.DY * n.DY) / n.Length;
63
64      return d <= Math.Max(penWidthX, penWidthY) / 2;
65    }
66
67    public override void Draw(Graphics graphics) {
68      graphics.DrawLine(Pen, Chart.TransformWorldToPixel(Start), Chart.TransformWorldToPixel(End));
69      base.Draw(graphics);
70    }
71  }
72}
Note: See TracBrowser for help on using the repository browser.