algorithmic modeling for Rhino
Hi,
I would like to have each and every point in a curve or Line or a list of points to have different colour of various shades of RGB.
I mean if a curve has around 200 points all 200 points should have different colours. If in case Curve can't be used a list of points can also be used to serve the purpose.
I am programming in C#..
Thanking in Advanced...
Regards,
Yajnavalkya Bandyopadhyay
Tags:
There's two relatively easy ways to do it and probably lots of hard ways. You can either keep generating random colours whenever you need another one, or, find some way to generate a logical progression of colours ahead of time and then randomise the list.
I'd go for the former. Create a new System.Random instance, create a HashSet<int>, and then whenever you need a new colour create random R, G, and B channels, construct your colour and see if the int-flavour already exists in the hash set; if not, add it to the hash and continue on your way, if yes, generate another random colour.
You can probably put the colours directly into the HashSet, but I know for sure that ints work.
// Utterly untested code...
Random random = new Random(1);
HashSet<int> hash = new HashSet<int>();
List<Color> colours = new List<Color>(1000)
for (int i = 0; i < 1000; i++)
{
Color c;
while (true)
{
int r = random.Next(0, 256);
int g = random.Next(0, 256);
int b = random.Next(0, 256);
c = Color.FromArgb(r, g, b);
int rgb = c.ToArgb();
if (hash.Contains(rgb)) continue;
hash.Add(rgb);
break;
}
colours.Add(c);
}
Hi, Thanks for the suggestion, but how to use that in a line! I am new to Grasshopper API and I am learning bits by bits.
Will the line be in a custom display.?
Can you please help me with it!
You output the list of colours and then you can use a [Custom Display] component to show your points using these colours.
If you also want to draw the points yourself, then you'll have to override the display methods on your component as well.
Try the attached.
Mind sharing the code please!
Double click the C# component to see the code.
I checked and the output is horrible even when I am increasing the number of point division.
Atleast can you show me a way by which I can do the ways, rest I have figured it out
1. Draw a line in grasshopper using C# (Start point and end point can be chosen randomly, I will just provide the length of the line)
2. Using a iteration I could be able to have all the coordinates present in the line.
If these two are provided I can use a custom display to do the rest
This is the code I have written, mind editing it?
private void RunScript(Curve C)
{
int l = 2000;
Rhino.Display.CustomDisplay cd = new Rhino.Display.CustomDisplay(true);
cd.Clear();Point3d a, b;
a = new Point3d(0, 0, 0);
b = new Point3d(0, 0, l);
Line x = new Line(a, b);
Curve m = x.ToNurbsCurve();
if (x == null) return;Point3d[] points;
m.DivideByCount(50, true, out points);//for (double itr = 0; itr < 50; itr = itr + 0.01)
//{
double frac = 50 / 230;
int itr = 0;
foreach (Point3d point in points)
{
while (true)
{
double imtr = (50 - itr) / frac;
itr++;
Color colour = ColorFromHSV(imtr, 1, 0.5);
int rgb = colour.ToArgb();
if (_hash.Contains(rgb))
continue;_hash.Add(rgb);
_points.Add(point);
_colours.Add(colour);
break;
}
//}
}
for (int i = 0; i < _points.Count; i++)
cd.AddPoint(_points[i], _colours[i]);
}// <Custom additional code>
private readonly HashSet<int> _hash = new HashSet<int>();
private readonly List<Point3d> _points = new List<Point3d>();
private readonly List<Color> _colours = new List<Color>();/// <summary>
/// This method will be called once every solution, before any calls to RunScript.
/// </summary>
public override void BeforeRunScript()
{
_hash.Clear();
_points.Clear();
_colours.Clear();
}
public static Color ColorFromHSV(double hue, double saturation, double value)
{
int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;
double f = hue / 60 - Math.Floor(hue / 60);value = value * 255;
int v = Convert.ToInt32(value);
int p = Convert.ToInt32(value * (1 - saturation));
int q = Convert.ToInt32(value * (1 - f * saturation));
int t = Convert.ToInt32(value * (1 - (1 - f) * saturation));if (hi == 0)
return Color.FromArgb(255, v, t, p);
else if (hi == 1)
return Color.FromArgb(255, q, v, p);
else if (hi == 2)
return Color.FromArgb(255, p, v, t);
else if (hi == 3)
return Color.FromArgb(255, p, q, v);
else if (hi == 4)
return Color.FromArgb(255, t, p, v);
else
return Color.FromArgb(255, v, p, q);
}
Gives this error
1. Value was either too large or too small for an Int32. (line: 0)
This line: double frac = 50 / 230;
integer 50 / integer 230 is zero. You should write
var frac=50d/230d;
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by