Dynamic line thickness.

This commit is contained in:
That_One_Nerd 2024-03-14 12:42:13 -04:00
parent bd70c17bf6
commit cd05a6829c
9 changed files with 37 additions and 37 deletions

View File

@ -6,12 +6,18 @@ public partial class PieChart : UserControl
{ {
public List<(Color, double)> Values { get; set; } public List<(Color, double)> Values { get; set; }
public float DpiFloat { get; private set; }
public PieChart() public PieChart()
{ {
SetStyle(ControlStyles.OptimizedDoubleBuffer, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.UserPaint, true);
Graphics tempG = CreateGraphics();
DpiFloat = (tempG.DpiX + tempG.DpiY) / 2;
tempG.Dispose();
Values = []; Values = [];
InitializeComponent(); InitializeComponent();
} }
@ -41,7 +47,7 @@ public partial class PieChart : UserControl
} }
// Draw the outline. // Draw the outline.
Pen outlinePartsPen = new(Color.FromArgb(unchecked((int)0xFF_202020)), 3); Pen outlinePartsPen = new(Color.FromArgb(unchecked((int)0xFF_202020)), DpiFloat * 3 / 192);
current = 0; current = 0;
foreach ((Color, double value) item in Values) foreach ((Color, double value) item in Values)
{ {
@ -53,7 +59,7 @@ public partial class PieChart : UserControl
} }
// Outline // Outline
Pen outlinePen = new(Color.FromArgb(unchecked((int)0xFF_202020)), 5); Pen outlinePen = new(Color.FromArgb(unchecked((int)0xFF_202020)), DpiFloat * 5 / 192);
g.DrawEllipse(outlinePen, rect); g.DrawEllipse(outlinePen, rect);
} }
} }

View File

@ -37,7 +37,7 @@ public partial class GraphColorPickerForm : Form
MessageLabel.Text = $"Pick a color for {able.Name}."; MessageLabel.Text = $"Pick a color for {able.Name}.";
// Add preset buttons. // Add preset buttons.
const int size = 48; int size = (int)(graph.DpiFloat * 48 / 192);
int position = 0; int position = 0;
foreach (uint cId in Graphable.DefaultColors) foreach (uint cId in Graphable.DefaultColors)
{ {

View File

@ -14,6 +14,8 @@ public partial class GraphForm : Form
public Float2 ScreenCenter { get; private set; } public Float2 ScreenCenter { get; private set; }
public Float2 Dpi { get; private set; } public Float2 Dpi { get; private set; }
public float DpiFloat { get; private set; }
public double ZoomLevel public double ZoomLevel
{ {
get => _zoomLevel; get => _zoomLevel;
@ -57,6 +59,9 @@ public partial class GraphForm : Form
Graphics tempG = CreateGraphics(); Graphics tempG = CreateGraphics();
Dpi = new(tempG.DpiX, tempG.DpiY); Dpi = new(tempG.DpiX, tempG.DpiY);
tempG.Dispose(); tempG.Dispose();
DpiFloat = (float)((Dpi.x + Dpi.y) / 2);
ables = []; ables = [];
ZoomLevel = 1; ZoomLevel = 1;
initialWindowPos = Location; initialWindowPos = Location;
@ -102,7 +107,7 @@ public partial class GraphForm : Form
// Draw horizontal/vertical quarter-axis. // Draw horizontal/vertical quarter-axis.
Brush quarterBrush = new SolidBrush(QuarterAxisColor); Brush quarterBrush = new SolidBrush(QuarterAxisColor);
Pen quarterPen = new(quarterBrush, 2); Pen quarterPen = new(quarterBrush, DpiFloat * 2 / 192);
for (double x = Math.Ceiling(MinVisibleGraph.x * 4 / axisScale) * axisScale / 4; x <= Math.Floor(MaxVisibleGraph.x * 4 / axisScale) * axisScale / 4; x += axisScale / 4) for (double x = Math.Ceiling(MinVisibleGraph.x * 4 / axisScale) * axisScale / 4; x <= Math.Floor(MaxVisibleGraph.x * 4 / axisScale) * axisScale / 4; x += axisScale / 4)
{ {
@ -119,7 +124,7 @@ public partial class GraphForm : Form
// Draw horizontal/vertical semi-axis. // Draw horizontal/vertical semi-axis.
Brush semiBrush = new SolidBrush(SemiAxisColor); Brush semiBrush = new SolidBrush(SemiAxisColor);
Pen semiPen = new(semiBrush, 2); Pen semiPen = new(semiBrush, DpiFloat * 2 / 192);
for (double x = Math.Ceiling(MinVisibleGraph.x / axisScale) * axisScale; x <= Math.Floor(MaxVisibleGraph.x / axisScale) * axisScale; x += axisScale) for (double x = Math.Ceiling(MinVisibleGraph.x / axisScale) * axisScale; x <= Math.Floor(MaxVisibleGraph.x / axisScale) * axisScale; x += axisScale)
{ {
@ -135,7 +140,7 @@ public partial class GraphForm : Form
} }
Brush mainLineBrush = new SolidBrush(MainAxisColor); Brush mainLineBrush = new SolidBrush(MainAxisColor);
Pen mainLinePen = new(mainLineBrush, 3); Pen mainLinePen = new(mainLineBrush, DpiFloat * 3 / 192);
// Draw the main axis (on top of the semi axis). // Draw the main axis (on top of the semi axis).
Int2 startCenterY = GraphSpaceToScreenSpace(new Float2(0, MinVisibleGraph.y)), Int2 startCenterY = GraphSpaceToScreenSpace(new Float2(0, MinVisibleGraph.y)),
@ -162,7 +167,8 @@ public partial class GraphForm : Form
{ {
IEnumerable<IGraphPart> lines = ables[i].GetItemsToRender(this); IEnumerable<IGraphPart> lines = ables[i].GetItemsToRender(this);
Brush graphBrush = new SolidBrush(ables[i].Color); Brush graphBrush = new SolidBrush(ables[i].Color);
foreach (IGraphPart gp in lines) gp.Render(this, g, graphBrush); Pen graphPen = new(graphBrush, DpiFloat * 3 / 192);
foreach (IGraphPart gp in lines) gp.Render(this, g, graphPen);
} }
base.OnPaint(e); base.OnPaint(e);

View File

@ -32,6 +32,10 @@ public partial class ViewCacheForm : Form
CachePie.Values.Add((able.Color, thisBytes)); CachePie.Values.Add((able.Color, thisBytes));
totalBytes += thisBytes; totalBytes += thisBytes;
int buttonHeight = (int)(refForm.DpiFloat * 46 / 192),
buttonWidth = (int)(refForm.DpiFloat * 92 / 192),
buttonSpaced = (int)(refForm.DpiFloat * 98 / 192);
if (index < labelCache.Count) if (index < labelCache.Count)
{ {
Label reuseLabel = labelCache[index]; Label reuseLabel = labelCache[index];
@ -45,9 +49,9 @@ public partial class ViewCacheForm : Form
Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right, Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right,
AutoEllipsis = true, AutoEllipsis = true,
ForeColor = able.Color, ForeColor = able.Color,
Location = new Point(0, labelCache.Count * 46), Location = new Point(0, labelCache.Count * buttonHeight),
Parent = SpecificCachePanel, Parent = SpecificCachePanel,
Size = new Size(SpecificCachePanel.Width - 98, 46), Size = new Size(SpecificCachePanel.Width - buttonSpaced, buttonHeight),
Text = $"{able.Name}: {thisBytes.FormatAsBytes()}", Text = $"{able.Name}: {thisBytes.FormatAsBytes()}",
TextAlign = ContentAlignment.MiddleLeft, TextAlign = ContentAlignment.MiddleLeft,
}; };
@ -59,9 +63,9 @@ public partial class ViewCacheForm : Form
Button newButton = new() Button newButton = new()
{ {
Anchor = AnchorStyles.Top | AnchorStyles.Right, Anchor = AnchorStyles.Top | AnchorStyles.Right,
Location = new Point(SpecificCachePanel.Width - 92, buttonCache.Count * 46), Location = new Point(SpecificCachePanel.Width - buttonWidth, buttonCache.Count * buttonHeight),
Parent = SpecificCachePanel, Parent = SpecificCachePanel,
Size = new Size(92, 46), Size = new Size(buttonWidth, buttonHeight),
Text = "Clear" Text = "Clear"
}; };
newButton.Click += (o, e) => EraseSpecificGraphable_Click(able); newButton.Click += (o, e) => EraseSpecificGraphable_Click(able);

View File

@ -4,5 +4,5 @@ namespace Graphing;
public interface IGraphPart public interface IGraphPart
{ {
public void Render(in GraphForm form, in Graphics g, in Brush brush); public void Render(in GraphForm form, in Graphics g, in Pen pen);
} }

View File

@ -18,15 +18,13 @@ public record struct GraphLine : IGraphPart
this.b = b; this.b = b;
} }
public readonly void Render(in GraphForm form, in Graphics g, in Brush brush) public readonly void Render(in GraphForm form, in Graphics g, in Pen pen)
{ {
if (!double.IsFinite(a.x) || !double.IsFinite(a.y) || if (!double.IsFinite(a.x) || !double.IsFinite(a.y) ||
!double.IsFinite(b.x) || !double.IsFinite(b.y)) return; !double.IsFinite(b.x) || !double.IsFinite(b.y)) return;
Int2 start = form.GraphSpaceToScreenSpace(a), Int2 start = form.GraphSpaceToScreenSpace(a),
end = form.GraphSpaceToScreenSpace(b); end = form.GraphSpaceToScreenSpace(b);
Pen pen = new(brush, 3);
g.DrawLine(pen, start, end); g.DrawLine(pen, start, end);
} }
} }

View File

@ -25,7 +25,7 @@ public record struct GraphRectangle : IGraphPart
max = max max = max
}; };
public void Render(in GraphForm form, in Graphics g, in Brush brush) public void Render(in GraphForm form, in Graphics g, in Pen pen)
{ {
if (!double.IsFinite(max.x) || !double.IsFinite(max.y) || if (!double.IsFinite(max.x) || !double.IsFinite(max.y) ||
!double.IsFinite(min.x) || !double.IsFinite(min.y)) return; !double.IsFinite(min.x) || !double.IsFinite(min.y)) return;
@ -40,6 +40,6 @@ public record struct GraphRectangle : IGraphPart
start.y - end.y); start.y - end.y);
if (size.x == 0 || size.y == 0) return; if (size.x == 0 || size.y == 0) return;
g.FillRectangle(brush, new Rectangle(start.x, end.y, size.x, size.y)); g.FillRectangle(pen.Brush, new Rectangle(start.x, end.y, size.x, size.y));
} }
} }

View File

@ -18,13 +18,13 @@ public record struct GraphUiCircle : IGraphPart
this.radius = radius; this.radius = radius;
} }
public readonly void Render(in GraphForm form, in Graphics g, in Brush brush) public readonly void Render(in GraphForm form, in Graphics g, in Pen pen)
{ {
if (!double.IsFinite(center.x) || !double.IsFinite(center.y) || if (!double.IsFinite(center.x) || !double.IsFinite(center.y) ||
!double.IsFinite(radius) || radius == 0) return; !double.IsFinite(radius) || radius == 0) return;
Int2 centerPix = form.GraphSpaceToScreenSpace(center); Int2 centerPix = form.GraphSpaceToScreenSpace(center);
g.FillEllipse(brush, new Rectangle(new Point(centerPix.x - radius, g.FillEllipse(pen.Brush, new Rectangle(new Point(centerPix.x - radius,
centerPix.y - radius), centerPix.y - radius),
new Size(radius * 2, radius * 2))); new Size(radius * 2, radius * 2)));
} }

View File

@ -10,28 +10,14 @@ internal static class Program
{ {
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);
GraphForm graph = new("One Of The Graphing Calculators Of All Time"); GraphForm graph = new("One Of The Graphing Calculators Of All Time");
Equation equ1 = new(x => Equation equ1 = new(x => -x * x + 2);
{
// Demonstrate the caching abilities of the software.
// This extra waiting is done every time the form requires a
// calculation done. At the start, it'll be laggy, but as you
// move around and zoom in, more pieces are cached, and when
// you reset, the viewport will be a lot less laggy.
// Remove this loop to make the equation fast again. I didn't
// slow the engine down much more with this improvement, so any
// speed decrease you might notice is likely this function.
for (int i = 0; i < 1_000_000; i++) ;
return -x * x + 2;
});
Equation equ2 = new(x => x); Equation equ2 = new(x => x);
Equation equ3 = new(x => -Math.Sqrt(x)); Equation equ3 = new(x => -Math.Sqrt(x));
SlopeField sf = new(2, (x, y) => (x * x - y * y) / x); graph.Graph(equ1, equ2, equ3);
graph.Graph(equ1, equ2, equ3, sf);
// You can also now view and reset caches in the UI by going to // You can also now view and reset caches in the UI by going to
// Misc > View Caches. // Misc > View Caches.