mirror of
https://github.com/ShareX/ShareX.git
synced 2024-09-30 01:07:21 +13:00
Added MathHelpers, angle info to ruler
This commit is contained in:
parent
1cdace0c9c
commit
961605f442
5 changed files with 242 additions and 7 deletions
|
@ -835,10 +835,5 @@ public static Size MeasureText(string text, Font font, int width)
|
|||
return g.MeasureString(text, font, width).ToSize();
|
||||
}
|
||||
}
|
||||
|
||||
public static double Distance(Point p1, Point p2)
|
||||
{
|
||||
return Math.Sqrt(Math.Pow(p2.X - p1.X, 2) + Math.Pow(p2.Y - p1.Y, 2));
|
||||
}
|
||||
}
|
||||
}
|
102
HelpersLib/Helpers/MathHelpers.cs
Normal file
102
HelpersLib/Helpers/MathHelpers.cs
Normal file
|
@ -0,0 +1,102 @@
|
|||
#region License Information (GPL v3)
|
||||
|
||||
/*
|
||||
ShareX - A program that allows you to take screenshots and share any file type
|
||||
Copyright (C) 2008-2014 ShareX Developers
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#endregion License Information (GPL v3)
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
|
||||
namespace HelpersLib
|
||||
{
|
||||
public static class MathHelpers
|
||||
{
|
||||
public const float RadianPI = 57.29578f; // 180.0 / Math.PI
|
||||
public const float DegreePI = 0.01745329f; // Math.PI / 180.0
|
||||
public const float TwoPI = 6.28319f; // Math.PI * 2
|
||||
|
||||
public static float RadianToDegree(float radian)
|
||||
{
|
||||
return radian * RadianPI;
|
||||
}
|
||||
|
||||
public static float DegreeToRadian(float degree)
|
||||
{
|
||||
return degree * DegreePI;
|
||||
}
|
||||
|
||||
public static Vector2 RadianToVector2(float radian)
|
||||
{
|
||||
return new Vector2((float)Math.Cos(radian), (float)Math.Sin(radian));
|
||||
}
|
||||
|
||||
public static Vector2 RadianToVector2(float radian, float length)
|
||||
{
|
||||
return RadianToVector2(radian) * length;
|
||||
}
|
||||
|
||||
public static Vector2 DegreeToVector2(float degree)
|
||||
{
|
||||
return RadianToVector2(DegreeToRadian(degree));
|
||||
}
|
||||
|
||||
public static Vector2 DegreeToVector2(float degree, float length)
|
||||
{
|
||||
return RadianToVector2(DegreeToRadian(degree), length);
|
||||
}
|
||||
|
||||
public static float Vector2ToRadian(Vector2 direction)
|
||||
{
|
||||
return (float)Math.Atan2(direction.Y, direction.X);
|
||||
}
|
||||
|
||||
public static float Vector2ToDegree(Vector2 direction)
|
||||
{
|
||||
return RadianToDegree(Vector2ToRadian(direction));
|
||||
}
|
||||
|
||||
public static float LookAtRadian(Vector2 pos1, Vector2 pos2)
|
||||
{
|
||||
return (float)Math.Atan2(pos2.Y - pos1.Y, pos2.X - pos1.X);
|
||||
}
|
||||
|
||||
public static Vector2 LookAtVector2(Vector2 pos1, Vector2 pos2)
|
||||
{
|
||||
return RadianToVector2(LookAtRadian(pos1, pos2));
|
||||
}
|
||||
|
||||
public static float LookAtDegree(Vector2 pos1, Vector2 pos2)
|
||||
{
|
||||
return RadianToDegree(LookAtRadian(pos1, pos2));
|
||||
}
|
||||
|
||||
public static float Distance(Vector2 pos1, Vector2 pos2)
|
||||
{
|
||||
return (float)Math.Sqrt(Math.Pow(pos2.X - pos1.X, 2) + Math.Pow(pos2.Y - pos1.Y, 2));
|
||||
}
|
||||
|
||||
public static float Lerp(float value1, float value2, float amount)
|
||||
{
|
||||
return value1 + (value2 - value1) * amount;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -88,6 +88,7 @@
|
|||
<Compile Include="ConvolutionMatrix.cs" />
|
||||
<Compile Include="ConvolutionMatrixManager.cs" />
|
||||
<Compile Include="FontSafe.cs" />
|
||||
<Compile Include="Helpers\MathHelpers.cs" />
|
||||
<Compile Include="Native\LayeredForm.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
|
@ -300,6 +301,7 @@
|
|||
<Compile Include="UpdateChecker\XMLUpdateChecker.cs" />
|
||||
<Compile Include="UpdateChecker\UpdateInfo.cs" />
|
||||
<Compile Include="Links.cs" />
|
||||
<Compile Include="Vector2.cs" />
|
||||
<Compile Include="WindowState.cs" />
|
||||
<Compile Include="XmlColor.cs" />
|
||||
<Compile Include="XmlFont.cs" />
|
||||
|
|
136
HelpersLib/Vector2.cs
Normal file
136
HelpersLib/Vector2.cs
Normal file
|
@ -0,0 +1,136 @@
|
|||
#region License Information (GPL v3)
|
||||
|
||||
/*
|
||||
ShareX - A program that allows you to take screenshots and share any file type
|
||||
Copyright (C) 2008-2014 ShareX Developers
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
as published by the Free Software Foundation; either version 2
|
||||
of the License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#endregion License Information (GPL v3)
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace HelpersLib
|
||||
{
|
||||
public struct Vector2
|
||||
{
|
||||
private float x, y;
|
||||
|
||||
public float X
|
||||
{
|
||||
get
|
||||
{
|
||||
return x;
|
||||
}
|
||||
set
|
||||
{
|
||||
x = value;
|
||||
}
|
||||
}
|
||||
|
||||
public float Y
|
||||
{
|
||||
get
|
||||
{
|
||||
return y;
|
||||
}
|
||||
set
|
||||
{
|
||||
y = value;
|
||||
}
|
||||
}
|
||||
|
||||
public static readonly Vector2 Empty = new Vector2();
|
||||
|
||||
public Vector2(float x, float y)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj is Vector2)
|
||||
{
|
||||
Vector2 v = (Vector2)obj;
|
||||
return v.x == x && v.y == y;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return base.GetHashCode();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return String.Format("X={0}, Y={1}", x, y);
|
||||
}
|
||||
|
||||
public static bool operator ==(Vector2 u, Vector2 v)
|
||||
{
|
||||
return u.x == v.x && u.y == v.y;
|
||||
}
|
||||
|
||||
public static bool operator !=(Vector2 u, Vector2 v)
|
||||
{
|
||||
return u != v;
|
||||
}
|
||||
|
||||
public static Vector2 operator +(Vector2 u, Vector2 v)
|
||||
{
|
||||
return new Vector2(u.x + v.x, u.y + v.y);
|
||||
}
|
||||
|
||||
public static Vector2 operator -(Vector2 u, Vector2 v)
|
||||
{
|
||||
return new Vector2(u.x - v.x, u.y - v.y);
|
||||
}
|
||||
|
||||
public static Vector2 operator *(Vector2 u, float a)
|
||||
{
|
||||
return new Vector2(a * u.x, a * u.y);
|
||||
}
|
||||
|
||||
public static Vector2 operator /(Vector2 u, float a)
|
||||
{
|
||||
return new Vector2(u.x / a, u.y / a);
|
||||
}
|
||||
|
||||
public static Vector2 operator -(Vector2 u)
|
||||
{
|
||||
return new Vector2(-u.x, -u.y);
|
||||
}
|
||||
|
||||
public static explicit operator Point(Vector2 u)
|
||||
{
|
||||
return new Point((int)Math.Round(u.x), (int)Math.Round(u.y));
|
||||
}
|
||||
|
||||
public static implicit operator Vector2(Point p)
|
||||
{
|
||||
return new Vector2(p.X, p.Y);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -200,8 +200,8 @@ protected override void Draw(Graphics g)
|
|||
if (RulerMode)
|
||||
{
|
||||
Point endPos = new Point(area.X + area.Width - 1, area.Y + area.Height - 1);
|
||||
areaText = string.Format("X: {0} / Y: {1} / X2: {2} / Y2: {3}\nWidth: {4} / Height: {5} / Length: {6:0.00}", area.X, area.Y, endPos.X, endPos.Y,
|
||||
area.Width, area.Height, Helpers.Distance(area.Location, endPos));
|
||||
areaText = string.Format("X: {0} / Y: {1} / X2: {2} / Y2: {3}\nWidth: {4} / Height: {5}\nDistance: {6:0.00} / Angle: {7:0.00}°", area.X, area.Y, endPos.X, endPos.Y,
|
||||
area.Width, area.Height, MathHelpers.Distance(area.Location, endPos), MathHelpers.LookAtDegree(area.Location, endPos));
|
||||
ImageHelpers.DrawTextWithOutline(g, areaText, new PointF(area.X + 15, area.Y + 15), textFont, Color.White, Color.Black);
|
||||
}
|
||||
else
|
||||
|
|
Loading…
Reference in a new issue