-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewport.cs
70 lines (60 loc) · 1.86 KB
/
Viewport.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace Graphiz
{
class Viewport
{
/// <summary>
/// The translated origin, which is at the same location as the actual viewport origin
/// </summary>
public Point Origin;
/// <summary>
/// The zoom scale for the viewport; 100 is no zoom, below is zoom in, above is zoom out
/// </summary>
public int Scale
{
get { return scale; }
set { if (value < 0) throw new ArgumentException("Scale cannot be below zero"); scale = value; }
}
private int scale;
public Viewport()
: this(Point.Empty, 100)
{ }
public Viewport(int x, int y, int scale)
: this(new Point(x, y), scale)
{ }
public Viewport(Point origin, int scale)
{
this.Origin = origin;
this.Scale = scale;
}
/// <summary>
/// Take a point from the screen and map it to a graph point
/// </summary>
public Point ToWorldPos(Point p)
{
return new Point(this.Origin.X + (int)(p.X * this.Scale) / 100,
this.Origin.Y + (int)(p.Y * this.Scale) / 100);
}
/// <summary>
/// Take a graph point and map it to the screen
/// </summary>
public Point FromWorldPos(Point p)
{
return new Point((int)((p.X - this.Origin.X) * 100) / (int)this.Scale,
(int)((p.Y - this.Origin.Y) * 100) / (int)this.Scale);
}
/// <summary>
/// Rescale a scalar
/// </summary>
/// <param name="i">
public int Rescale(int i)
{
return (i * 100) / Scale;
}
}
}