Small improvement

This commit is contained in:
CreepyCrafter24 2020-01-10 16:30:06 +01:00
parent 6097438ea9
commit a8b2dc1523
3 changed files with 53 additions and 47 deletions

View File

@ -1,19 +1,20 @@
using NCalc2; using System;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Drawing2D; using System.Drawing.Drawing2D;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.Linq; using System.Linq;
using System.Windows.Forms; using System.Windows.Forms;
using NCalc2;
namespace PowerCalc namespace PowerCalc
{ {
public partial class MainForm : Form public partial class MainForm : Form
{ {
public decimal offX = 0; private Point offStart = Point.Empty;
public decimal offY = 0; public decimal offX;
Point offStart = Point.Empty; public decimal offY;
public MainForm() public MainForm()
{ {
InitializeComponent(); InitializeComponent();
@ -41,7 +42,7 @@ namespace PowerCalc
private void log(object text) private void log(object text)
{ {
logBox.Lines = new string[] { text.ToString() }.Concat(logBox.Lines).Where((s, i) => i < 30).ToArray(); logBox.Lines = new[] {text.ToString()}.Concat(logBox.Lines).Where((s, i) => i < 30).ToArray();
#if DEBUG #if DEBUG
Console.WriteLine(text); Console.WriteLine(text);
#endif #endif
@ -59,16 +60,20 @@ namespace PowerCalc
g.PixelOffsetMode = PixelOffsetMode.None; g.PixelOffsetMode = PixelOffsetMode.None;
g.CompositingMode = CompositingMode.SourceCopy; g.CompositingMode = CompositingMode.SourceCopy;
List<Tuple<Color, List<PointF>, Expression>> lines = new List<Tuple<Color, List<PointF>, Expression>> List<Tuple<Color, List<PointF>, Expression>> lines = new List<Tuple<Color, List<PointF>, Expression>>
{ {
new Tuple<Color, List<PointF>, Expression>(Color.Red, new List<PointF>(), new Expression(calcBox1.Text)), new Tuple<Color, List<PointF>, Expression>(Color.Red, new List<PointF>(),
new Tuple<Color, List<PointF>, Expression>(Color.FromArgb(255, 128, 0), new List<PointF>(), new Expression(calcBox2.Text)), new Expression(calcBox1.Text)),
new Tuple<Color, List<PointF>, Expression>(Color.FromArgb(0, 192, 0), new List<PointF>(), new Expression(calcBox3.Text)), new Tuple<Color, List<PointF>, Expression>(Color.FromArgb(255, 128, 0), new List<PointF>(),
new Tuple<Color, List<PointF>, Expression>(Color.FromArgb(0, 0, 192), new List<PointF>(), new Expression(calcBox4.Text)) new Expression(calcBox2.Text)),
}; new Tuple<Color, List<PointF>, Expression>(Color.FromArgb(0, 192, 0), new List<PointF>(),
for (int i = (int)(offX % 10); i < evalBox.Width; i += 10) new Expression(calcBox3.Text)),
g.DrawLine((offX - i == 0) ? Pens.Gray : Pens.LightGray, i, 0, i, evalBox.Height); new Tuple<Color, List<PointF>, Expression>(Color.FromArgb(0, 0, 192), new List<PointF>(),
for (int i = evalBox.Height + (int)(offY % 10); i > 0; i -= 10) new Expression(calcBox4.Text))
g.DrawLine((evalBox.Height + (offY - i) == 0) ? Pens.Gray : Pens.LightGray, 0, i, evalBox.Width, i); };
for (int i = (int) (offX % 10); i < evalBox.Width; i += 10)
g.DrawLine(offX - i == 0 ? Pens.Gray : Pens.LightGray, i, 0, i, evalBox.Height);
for (int i = evalBox.Height + (int) (offY % 10); i > 0; i -= 10)
g.DrawLine(evalBox.Height + (offY - i) == 0 ? Pens.Gray : Pens.LightGray, 0, i, evalBox.Width, i);
lines.ForEach(s => lines.ForEach(s =>
{ {
s.Item3.Parameters.Add("Pi", Math.PI); s.Item3.Parameters.Add("Pi", Math.PI);
@ -77,53 +82,51 @@ namespace PowerCalc
s.Item3.Parameters.Add("e", Math.E); s.Item3.Parameters.Add("e", Math.E);
s.Item3.Parameters.Add("E", Math.E); s.Item3.Parameters.Add("E", Math.E);
for (double i = 0; i < evalBox.Width; i++) for (double i = 0; i < evalBox.Width; i++)
{
try try
{ {
s.Item3.Parameters["x"] = (i - (double)offX) / 10; s.Item3.Parameters["x"] = (i - (double) offX) / 10;
double val = -1; double val = -1;
object tmp = s.Item3.Evaluate(); object tmp = s.Item3.Evaluate();
if (tmp.GetType() == typeof(bool)) if (tmp.GetType() == typeof(bool))
val = (bool)tmp ? 1 : 0; val = (bool) tmp ? 1 : 0;
else if (tmp.GetType() == typeof(byte)) else if (tmp.GetType() == typeof(byte))
val = (byte)tmp; val = (byte) tmp;
else if (tmp.GetType() == typeof(sbyte)) else if (tmp.GetType() == typeof(sbyte))
val = (sbyte)tmp; val = (sbyte) tmp;
else if (tmp.GetType() == typeof(short)) else if (tmp.GetType() == typeof(short))
val = (short)tmp; val = (short) tmp;
else if (tmp.GetType() == typeof(ushort)) else if (tmp.GetType() == typeof(ushort))
val = (ushort)tmp; val = (ushort) tmp;
else if (tmp.GetType() == typeof(int)) else if (tmp.GetType() == typeof(int))
val = (int)tmp; val = (int) tmp;
else if (tmp.GetType() == typeof(uint)) else if (tmp.GetType() == typeof(uint))
val = (uint)tmp; val = (uint) tmp;
else if (tmp.GetType() == typeof(long)) else if (tmp.GetType() == typeof(long))
val = (long)tmp; val = (long) tmp;
else if (tmp.GetType() == typeof(ulong)) else if (tmp.GetType() == typeof(ulong))
val = (ulong)tmp; val = (ulong) tmp;
else if (tmp.GetType() == typeof(float)) else if (tmp.GetType() == typeof(float))
val = (float)tmp; val = (float) tmp;
else if (tmp.GetType() == typeof(double)) else if (tmp.GetType() == typeof(double))
val = (double)tmp; val = (double) tmp;
else if (tmp.GetType() == typeof(decimal)) else if (tmp.GetType() == typeof(decimal))
val = (double)(decimal)tmp; val = (double) (decimal) tmp;
else else
log("Type mismatch! (" + tmp.GetType().ToString() + ")"); log("Type mismatch! (" + tmp.GetType() + ")");
float val1 = Convert.ToSingle(val); float val1 = Convert.ToSingle(val);
float loc = Convert.ToSingle(evalBox.Height - ((val1 * 10) - (float)offY)); float loc = Convert.ToSingle(evalBox.Height - ((val1 * 10) - (float) offY));
if (loc >= 0 && loc < evalBox.Height) if (loc >= 0 && loc < evalBox.Height)
s.Item2.Add(new PointF(Convert.ToSingle(i), loc)); s.Item2.Add(new PointF(Convert.ToSingle(i), loc));
} }
catch (Exception e1) catch (Exception e1)
{ {
#if DEBUG #if DEBUG
log("Value error: " + e1.ToString()); log("Value error: " + e1);
#else #else
log("Value error: " + e1.Message); log("Value error: " + e1.Message);
#endif #endif
break; break;
} }
}
g.DrawLines(new Pen(s.Item1), s.Item2.ToArray()); g.DrawLines(new Pen(s.Item1), s.Item2.ToArray());
}); });
g.Flush(); g.Flush();
@ -142,13 +145,14 @@ namespace PowerCalc
s = s.Remove(4, s.Length - 4); s = s.Remove(4, s.Length - 4);
return s; return s;
}) })
)); ));
} }
} }
private void evalBox_MouseMove(object sender, MouseEventArgs e) private void evalBox_MouseMove(object sender, MouseEventArgs e)
{ {
coordLabel.Text = new Point((int)Math.Round((double)(e.X - offX) / 10d), (int)Math.Round((double)((evalBox.Height - offY) - e.Y) / 10d)).ToString(); coordLabel.Text = new Point((int) Math.Round((double) (e.X - offX) / 10d),
(int) Math.Round((double) ((evalBox.Height + offY) - e.Y) / 10d)).ToString();
if ((e.Button & MouseButtons.Left) == MouseButtons.Left) if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{ {
offX += e.X - offStart.X; offX += e.X - offStart.X;
@ -158,12 +162,14 @@ namespace PowerCalc
} }
} }
private void evalBox_MouseLeave(object sender, EventArgs e) => coordLabel.Text = Point.Empty.ToString().Replace("0", ""); private void evalBox_MouseLeave(object sender, EventArgs e) =>
coordLabel.Text = Point.Empty.ToString().Replace("0", "");
private void evalBox_MouseDown(object sender, MouseEventArgs e) => offStart = e.Location; private void evalBox_MouseDown(object sender, MouseEventArgs e) => offStart = e.Location;
private void saveButton_Click(object sender, EventArgs e) private void saveButton_Click(object sender, EventArgs e)
{ {
ImageFormat[] formats = new ImageFormat[] ImageFormat[] formats =
{ {
ImageFormat.Bmp, ImageFormat.Bmp,
ImageFormat.Emf, ImageFormat.Emf,
@ -176,12 +182,15 @@ namespace PowerCalc
SaveFileDialog dlg = new SaveFileDialog SaveFileDialog dlg = new SaveFileDialog
{ {
FileName = "Graph", FileName = "Graph",
Filter = string.Join("|", formats.Select(s => s.ToString() + " Image|*." + (s.ToString() == "Jpeg" ? "jpg" : s.ToString().ToLower())).ToArray()) Filter = string.Join("|",
formats.Select(s => s + " Image|*." + (s.ToString() == "Jpeg" ? "jpg" : s.ToString().ToLower()))
.ToArray())
}; };
if (dlg.ShowDialog() == DialogResult.OK) if (dlg.ShowDialog() == DialogResult.OK)
{ {
Bitmap bmp = new Bitmap(evalBox.Width, evalBox.Height); Bitmap bmp = new Bitmap(evalBox.Width, evalBox.Height);
evalBox_Paint(evalBox, new PaintEventArgs(Graphics.FromImage(bmp), new Rectangle(Point.Empty, bmp.Size))); evalBox_Paint(evalBox,
new PaintEventArgs(Graphics.FromImage(bmp), new Rectangle(Point.Empty, bmp.Size)));
bmp.Save(dlg.FileName, formats[dlg.FilterIndex - 1]); bmp.Save(dlg.FileName, formats[dlg.FilterIndex - 1]);
} }
} }

View File

@ -9,12 +9,14 @@
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<RootNamespace>PowerCalc</RootNamespace> <RootNamespace>PowerCalc</RootNamespace>
<AssemblyName>PowerCalc</AssemblyName> <AssemblyName>PowerCalc</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion> <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic> <Deterministic>true</Deterministic>
<NuGetPackageImportStamp> <NuGetPackageImportStamp>
</NuGetPackageImportStamp> </NuGetPackageImportStamp>
<LangVersion>8</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>

View File

@ -1,14 +1,9 @@
using System; using System.Windows.Forms;
using System.Windows.Forms;
namespace PowerCalc namespace PowerCalc
{ {
internal static class Program internal static class Program
{ {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
private static void Main() private static void Main()
{ {
Application.EnableVisualStyles(); Application.EnableVisualStyles();