Skip to content

Instantly share code, notes, and snippets.

@amitkhare
Last active January 28, 2020 17:14
Show Gist options
  • Save amitkhare/e5d18fd08d577cb4e461c957eb91ecdd to your computer and use it in GitHub Desktop.
Save amitkhare/e5d18fd08d577cb4e461c957eb91ecdd to your computer and use it in GitHub Desktop.
Wasp3D, C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace amitkhare
{
struct Float2
{
public float x;// { get; private set; }
public float y;// { get; private set; }
public float width;// { get; private set; }
public float height;// { get; private set; }
public Float2(float _x, float _y)
{
x = _x;
y = _y;
width = x;
height = y;
}
}
class Point {
public Float2 value { get; private set; }
public Float2 position { get; private set; }
public Point(float x, float y)
{
this.value = new Float2(x, y);
this.position = new Float2(-1, -1);
}
public void SetPosition(Float2 position)
{
this.position = position;
}
public override string ToString()
{
// x y l |
return value.x + " " + value.y + " l |";
}
}
class LineGraph
{
public List<Point> points { get; private set; }
public Float2 dimention { get; private set; }
private bool RemapYValues = true;
private float ElevateY = 10f;
private float MaxValue = 0.0f;
public LineGraph(List<float> dataPoints, float width = 1000f, float height = 500f, bool RemapYValues = true, float ElevateY = 10f, float MaxValue = 0.0f)
{
dimention = new Float2(width, height);
this.RemapYValues = RemapYValues;
this.ElevateY = ElevateY;
this.MaxValue = (MaxValue == 0.0f) ? dataPoints.Max(): MaxValue;
this.points = new List<Point>();
for (int i = 0; i < dataPoints.Count; i++)
{
Point pt = new Point(i + 1, dataPoints[i]);
pt.SetPosition(GeneratePosition(i, dataPoints));
this.points.Add(pt);
}
}
public override string ToString()
{
string str = "";
// add first blank point
if(!RemapYValues) str += new Point(1f, MaxValue).ToString();
if(!RemapYValues) str += new Point(1f, 0f).ToString();
foreach (Point point in points)
{
str += point.ToString();
}
// add last blank point
if(!RemapYValues) str += new Point(points.Count, 0f).ToString();
//if(!RemapYValues) str += new Point(points.Count, MaxValue).ToString();
str += "A";
return str;
}
private Float2 GeneratePosition(int idx, List<float> dataPoints) {
float posX = ((dimention.width / (dataPoints.Count - 1)) * idx) - (dimention.width / 2f);
float posY = 0.0f;
if (RemapYValues) {
posY = remapRange(dataPoints[idx], dataPoints.Max(), dataPoints.Min(), dimention.height, 0.0f);
} else {
posY = ((dataPoints[idx] * this.dimention.height / MaxValue) + 0f);
//posY = ((dataPoints[idx] * this.dimention.height / dataPoints.Max()) + 0f);
}
posY += ElevateY;
return new Float2(posX, posY);
}
private float remapRange(float value ,float OldMAX, float OldMIN, float NewMAX, float NewMIN) {
float OldRange = (OldMAX - OldMIN);
float NewRange = (NewMAX - NewMIN);
float NewValue = (((value - OldMIN) * NewRange) / OldRange) + NewMIN;
return NewValue;
}
}
}
@amitkhare
Copy link
Author

amitkhare commented Jan 21, 2020

            List<float> DataValues= new List<float>(); ;

            DataValues.Add(100f);
            DataValues.Add(200f);
            DataValues.Add(300f);
            DataValues.Add(400f);
            DataValues.Add(500f);
            
            
            amitkhare.LineGraph lineGraph = new amitkhare.LineGraph(DataValues.Take(WInteger.PointCount).ToList(), 1000f, 500f, false, 10f);
            
            WString.GraphData = lineGraph.ToString();
            
            int i = 0;
            foreach (amitkhare.Point point in lineGraph.points)
            {
                // point
                MarkPossitions[i].SetValue(point.position.x,point.position.y, 0f);
                i++;
            }

@amitkhare
Copy link
Author

 private void CalculateData()
		{            
            
            
			float MaxValue1 = G1_DataValues.Take(WInteger.PointCount).Max();
			float MaxValue2 = G2_DataValues.Take(WInteger.PointCount).Max();
			float MaxValue = (MaxValue1 > MaxValue2) ? MaxValue1 : MaxValue2;
			MaxValue = (WInteger.NumberOfGraphs > 1) ? MaxValue : G1_DataValues.Take(WInteger.PointCount).Max();
			
            amitkhare.LineGraph lineGraphG1 = new amitkhare.LineGraph(G1_DataValues.Take(WInteger.PointCount).ToList(), 1000f, 500f, false, 13f, MaxValue);
            amitkhare.LineGraph lineGraphG2 = new amitkhare.LineGraph(G2_DataValues.Take(WInteger.PointCount).ToList(), 1000f, 500f, false, 13f, MaxValue);
				
			WString.GraphData_1 = lineGraphG1.ToString();
            WString.GraphData_2 = lineGraphG2.ToString();
            
			for (int i = 0; i < lineGraphG1.points.Count; i++) {
				G1_MarkPositions[i].SetValue(lineGraphG1.points[i].position.x,lineGraphG1.points[i].position.y, 0f);
				G2_MarkPositions[i].SetValue(lineGraphG2.points[i].position.x,lineGraphG2.points[i].position.y, 0f);
			}
			
			WFloat.MatteRectHeight_Left_G1 = lineGraphG1.points[0].position.y;
			WFloat.MatteRectHeight_Left_G2 = lineGraphG2.points[0].position.y;
			WFloat.MatteRectHeight_Right_G1 = lineGraphG1.points[lineGraphG1.points.Count -1].position.y;
			WFloat.MatteRectHeight_Right_G2 = lineGraphG2.points[lineGraphG2.points.Count -1].position.y;
			
		}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment