Hello!
I am currently learning about C#, and I am wondering whether some of the components could be replaced by code in C#.
I am working on this script, and I want to write a program that will perform the same function as gradient and return the material. I have already replaced three of the components, yet I am struggling with this one. Can you please help ?
The vague idea of what I want to do:
List<Line> lines = new List<Line>();
lines.Add(inputLine);
DivideAndRotate(inputLine, ref lines, angle, minLength);
Lines = lines;
//Substituting the length component
List<double> lengths = new List<double>();
foreach (Line itemLine in lines)
{
lengths.Add(itemLine.Length);
}
Lengths = lengths;
//substituting the Bounds component
double minValueBounds = lengths.Min();
double maxValueBounds = lengths.Max();
Interval bounds = new Interval(minValueBounds, maxValueBounds);
Domain = bounds;
//substituting the remap component
List<double> remaps = new List<double>();
foreach(double length in lengths )
{
remaps.Add(RemapValues(length, minValueBounds, maxValueBounds));
}
Remap = remaps;
//I am not really sure if I should use one of these methods, and which one?
// How should I do the Gradient function?
//Rhino.Display.DisplayMaterial material = new Rhino.Display.DisplayMaterial();
//material = material.Specular(System.Drawing.Color.Lime);
Rhino.DocObjects.Material material = new Rhino.DocObjects.Material();
//material.EmissionColor(System.Drawing.Color.Lime);
material = Rhino.DocObjects.Material.DefaultMaterial;
material.SpecularColor = System.Drawing.Color.Lime;
//material.Name = "MyMaterial";
Material = material;
@Michael_Pryor
Thanks, for your quick reply.
Answering your question, I do not want to make a new C# component that performs the same function, I just want to add into my already existing code.
My input (in the code is named remaps) would be the remapped numbers, this I already programmed into C#, now I just want to colour them differently for each value, therefore, I need to translate the values into colours, if it is possible using the same presets as the Gradient component has.
@Mahdiyar
Hi!
Thank you very much for the time you spent working on my problem, I saw that you put in a lot of effort into it,I appreciate this very much! When I saw your code, I couldn’t believe how organized it was, thank you for improving my remaining code.
I have a few questions regarding these lines:
I am not quite sure why these lines are necessary for the code, as we already defined that _parameters = remaps, and _lines = lines. So why should we now write them now above the function?
Here I don’t quite understand what this line of code is doing. I think it is displaying the colours on the geometry, but I am not quite sure…
P.S. Thanks for showing me Linq, I didn’t know that you could shorten the code so much with it!!!
You are probably just used to write code in methods, meaning you create local variables and modify them. But as soon as you declare them within a method, they only live within this scope.
As soon as you need to pass variables across other methods, you get a problem. One option is to pass them as argument to another function. But this is not very practical, because sometimes you don‘t have access to modify the signature of another method or you don‘t want to pass dozens of arguments or you don‘t want to call this method directly… So you can declare this as a field or property instead and call them from any instance method (=non static).
private int _somePrivateField = 42;
public int SomeProperty {get{return _somePrivateField;}}
public void DoSomething()
{ _somePrivateField += 24;}