Files
winformGraphs/TestWinFormsDataVis/Form1.cs

220 lines
6.3 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestWinFormsDataVis
{
public partial class Form1 : Form
{
Pen myPen = new Pen(Color.Black);
Pen myCPenR = new Pen(Color.Red);
Pen myCPenG = new Pen(Color.Green);
Graphics g = null;
List<StraightLine> koordCross = new();
List<TwoPoints> CurveSin = new();
List<TwoPoints> CurveCos = new();
public bool CrossDrawn { get; set; } = false;
public bool CurvesDrawn { get; set; } = false;
static int _yMid = 0;
static int _yxPos = 3;
static int _yTop = 0;
static int _yStep = 0;
static long _yScaleStep = 0;
static int _yBottom = 0;
static int _xRight = 0;
static int countNum = 0;
static double _timeX = 0;
static int _timeXPosOld = 0;
static int _timeXPos = 0;
static int _timeYSin = 0;
static int _timeYCos = 0;
public Form1()
{
InitializeComponent();
_yMid = pnlCanvas.Height / 2;
_yTop = pnlCanvas.Height;
_yBottom = 0;
_xRight = pnlCanvas.Width;
createKoordCross(koordCross, 3000, -3000);
pnlCanvas.Refresh();
_timeYCos = _yMid;
_timeYSin = _yMid;
}
private static void createKoordCross(List<StraightLine> koordCross, decimal maxVal, decimal minVal)
{
// Y-axel
koordCross.Add(new StraightLine { start_x = _yxPos, start_y = _yTop, end_x = _yxPos, end_y = _yBottom });
// X-axel
koordCross.Add(new StraightLine { start_x = _yxPos - 2, start_y = _yMid, end_x = _xRight, end_y = _yMid });
_yScaleStep = scaleStep(maxVal, 8);
_yStep = (int)_yScaleStep * _yMid / (int)maxVal;
var _yVal = _yMid;
while (_yVal < _yTop)
{
koordCross.Add(new StraightLine { start_x = _yxPos - 2, start_y = _yVal, end_x = _yxPos + 2, end_y = _yVal });
_yVal += _yStep;
}
_yVal = _yMid;
while (_yVal > 0)
{
koordCross.Add(new StraightLine { start_x = _yxPos - 2, start_y = _yVal, end_x = _yxPos + 2, end_y = _yVal });
_yVal -= _yStep;
}
}
private static long scaleStep(decimal maxValue, int maxSteps)
{
long stepValue = nextValue(true);
long tmpValue = stepValue;
int _maxSteps = (int)((int)maxValue / stepValue);
while (_maxSteps > maxSteps)
{
stepValue = tmpValue * nextValue();
_maxSteps = (int)((int)maxValue / stepValue);
tmpValue = stepValue == tmpValue * 10 ? stepValue : tmpValue;
}
return stepValue;
}
private static int nextValue(bool first = false)
{
if (first)
{
countNum = 1;
}
else
{
switch (countNum)
{
case 1:
{
countNum = 2;
break;
}
case 2:
{
countNum = 5;
break;
}
case 5:
{
countNum = 10;
break;
}
case 10:
{
countNum = 2;
break;
}
}
}
return countNum;
}
private void drawKoordCrossLines()
{
myPen.Color = Color.Black;
foreach (var x in koordCross)
{
Point[] points =
{
new Point(x.start_x,x.start_y),
new Point(x.end_x,x.end_y)
};
g.DrawLines(myPen, points);
}
//CrossDrawn = true;
}
private void executeCurves()
{
//myPen.Color = Color.Red;
//myPen.Color = Color.Green;
foreach (var tp in CurveCos)
{
g.DrawLines(myCPenG, tp.drwPoints());
}
foreach (var tp in CurveSin)
{
g.DrawLines(myCPenR, tp.drwPoints());
}
}
private void drawCurves()
{
var _prevYSin = _timeYSin;
var _prevYCos = _timeYCos;
_timeX = _timeX + .017453292519;
_timeYSin = _yMid + (int)(Math.Sin(_timeX) * -1 * _yMid);
CurveSin.Add(new TwoPoints { X1 = _timeXPosOld, Y1 = _prevYSin, X2 = _timeXPos, Y2 = _timeYSin });
//foreach (var tp in CurveSin)
//{
// g.DrawLines(myPen, tp.drwPoints());
//}
//Debug.WriteLine($"p1 (x1,y1,x2,y2) -> {points[0].X},{points[0].Y},{points[1].X},{points[1].Y} ");
_timeYCos = _yMid + (int)(Math.Cos(_timeX) * _yMid);
CurveSin.Add(new TwoPoints { X1 = _timeXPosOld, Y1 = _prevYCos, X2 = _timeXPos, Y2 = _timeYCos });
//foreach (var tp in CurveCos)
//{
// g.DrawLines(myPen, tp.drwPoints());
//}
//Debug.WriteLine($"p2 (x1,y1,x2,y2) -> {xpoints[0].X},{xpoints[0].Y},{xpoints[1].X},{xpoints[1].Y} ");
_timeXPosOld = _timeXPos;
CurvesDrawn = true;
}
private void pnlCanvas_Paint(object sender, PaintEventArgs e)
{
//if (!CrossDrawn)
//{
myPen.Width = 1;
g = pnlCanvas.CreateGraphics();
drawKoordCrossLines();
//}
if (CurvesDrawn)
{
executeCurves();
}
//drawCurves();
}
private void paintTimer_Tick(object sender, EventArgs e)
{
_timeXPos += 1;
drawCurves();
pnlCanvas.Refresh();
}
}
}