Skip to content

Instantly share code, notes, and snippets.

@amitkhare
Last active February 3, 2020 07:51
Show Gist options
  • Save amitkhare/ed3ccc11f380c1e62d1db4d17e0778fc to your computer and use it in GitHub Desktop.
Save amitkhare/ed3ccc11f380c1e62d1db4d17e0778fc to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace amitkhare
{
class W3DGraph
{
private List<float> Values;
public List<Point> Points { get; private set; }
public float RemapMin { get; private set; }
public float MaxValue { get; private set; }
public float width { get; private set; }
public float height { get; private set; }
public float totalAngle { get; private set; }
private bool LineRemapMinMax = false;
private void Init(List<float> Values, float width, float height, float MaxValue, float RemapMin, float totalAngle)
{
this.Values = Values;
this.width = width;
this.height = height;
this.MaxValue = (Values.Max() > MaxValue) ? Values.Max() : MaxValue;
this.RemapMin = RemapMin;
this.totalAngle = totalAngle;
this.generatePoints();
}
private void generatePoints() {
Points = new List<Point>();
for (int i = 0; i < Values.Count; i++)
{
Float2 value = new Float2(i, Values[i]);
Float2 position = generatePointPosition(i, value);
float angle = (value.y / Values.Sum()) * totalAngle;
Points.Add(new Point(value, position, angle));
}
}
public W3DGraph SetLineRemapMinMax(bool status)
{
this.LineRemapMinMax = status;
this.generatePoints();
return this;
}
public string ToLineGraphString()
{
string str = "";
// add first blank point
if (LineRemapMinMax) str += new Point(new Float2(0f, RemapMin), new Float2(0f, 0f)).ToLineGraphString();
foreach (Point point in Points)
{
str += point.ToLineGraphString();
}
// add last blank point
if (LineRemapMinMax) str += new Point( new Float2(Values.Count - 1, MaxValue), new Float2(0,0) ).ToLineGraphString();
str += "A";
return str;
}
private Float2 generatePointPosition(int idx, Float2 value) {
float posX = ((width / (Values.Count - 1)) * idx) - (width / 2f);
float posY;
if(LineRemapMinMax){
posY = remapRange(Values[idx], MaxValue, RemapMin, height, RemapMin);
} else {
posY = remapRange(Values[idx], Values.Max(), Values.Min(), height, RemapMin);
}
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;
}
//--------------------------------------------------------
//---------------- Constructors --------------------------
//--------------------------------------------------------
public W3DGraph(List<float> Values, float totalAngle = 360.0f)
{
Init(Values, 0f, 0f, 0f, 0f, totalAngle);
}
public W3DGraph(List<float> Values, float width, float height)
{
Init(Values, width, height, Values.Max(), 0f, 0f);
}
public W3DGraph(List<float> Values, float width, float height, float MaxValue)
{
Init(Values, width, height, MaxValue, 0f, 0f);
}
public W3DGraph(List<float> Values, float width, float height, float MaxValue, float RemapMin)
{
Init(Values, width, height, MaxValue, RemapMin, 0f);
}
public W3DGraph(List<float> Values, float width, float height, float MaxValue, float RemapMin, float totalAngle)
{
Init(Values, width, height, MaxValue, RemapMin, totalAngle);
}
}
class Point
{
public Float2 Position { get; private set; }
public Float2 Value { get; private set; }
public float Angle { get; private set; }
public Point(Float2 value, Float2 position, float angle = 0.0f)
{
Position = position;
Value = value;
Angle = angle;
}
public string ToLineGraphString()
{
// x y l |
//return (Value.x + 1 ) + " " + Value.y + " l |";
return Value.x + " " + Value.y + " l |";
}
}
struct Float2
{
public float x;
public float y;
public Float2(float _x, float _y)
{
x = _x;
y = _y;
}
}
}
@amitkhare
Copy link
Author


                List<float> values;
		private void Calculate()
		{
			values = new List<float>();
			values.Add(10f);
			values.Add(20f);
			values.Add(30f);
			values.Add(20f);
			values.Add(10f);
			
			W3DGraph graph = new W3DGraph(values, 1000f, 500f, values.Max(), values.Min(), 360f).SetLineRemapMinMax(WBoolean.StartEndCap);
			
			WString.LineGraph = graph.ToLineGraphString();
			MRect_1.RectangleHeight = graph.Points[0].Position.y;
			MRect_2.RectangleHeight = ( 500f - graph.Points[values.Count - 1].Position.y);
			
			pt_1.Translation = new Vector3(graph.Points[0].Position.x, graph.Points[0].Position.y, 0f);
			pt_2.Translation = new Vector3(graph.Points[1].Position.x, graph.Points[1].Position.y, 0f);
			pt_3.Translation = new Vector3(graph.Points[2].Position.x, graph.Points[2].Position.y, 0f);
			pt_4.Translation = new Vector3(graph.Points[3].Position.x, graph.Points[3].Position.y, 0f);
			pt_5.Translation = new Vector3(graph.Points[4].Position.x, graph.Points[4].Position.y, 0f);

		}

@amitkhare
Copy link
Author

<table totalrows="1">
  <data datasource="">
    <col cid="0" colname="visibility">
      <row rid="0" text="-1" />
    </col>
    <col cid="1" colname="value">
      <row rid="0" text="100" />
    </col>
    <col cid="2" colname="color">
      <row rid="0" text="1,0,0,1" />
    </col>
    <col cid="3" colname="offset">
      <row rid="0" text="0" />
    </col>
    <col cid="4" colname="pinangleoffset">
      <row rid="0" text="0.000000" />
    </col>
    <col cid="5" colname="pinradiusoffset">
      <row rid="0" text="0.000000" />
    </col>
    <col cid="6" colname="pindepthoffset">
      <row rid="0" text="0.000000" />
    </col>
    <col cid="7" colname="center">
      <row rid="0" text="0.000000,0.000000,0.000000" />
    </col>
  </data>
</table>

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