Graduated colour depending on offset from original position lazarus / 70-OpenGL ---------------------------------------------------------------------- ranny · 2024-07-21 07:35 Hi, I have been working on a program that calculates stresses and deflection of a structure under various loading conditions.  Currently I can draw the model and I can draw, at the moment, the deflected state.  I know what the maximum deflection is for any part of the structure and what I would like is to have the deflection image display the shape with graduated colour, say, from blue to red, based on amount of deflection. On the attached image the original framework is shown and the deflected shape is in red.  So what I would like is for any node that is deflected the elements between will be coloured based on how much it is deflected from its original position. ---------------------------------------------------------------------- wp · 2024-07-21 09:39 If you do this drawing in OpenGL you can specify a color after every vertex of the line segments, and OpenGL will interpolate colors between them: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i       glBegin(GL_LINES);        glColor3f(1, 0, 0);        glVertex3f(-1, -1, 0);        glColor3f(0, 0, 1);        glVertex3f(0, 1, 1);      glEnd; If you draw with standard LCL graphics canvas commands, you will have to write your own Line() method. If you draw with BGRABitmap you probably find a corresponding line-drawing method in the codebase. ---------------------------------------------------------------------- ranny · 2024-07-21 10:21 Hi wp, I was aware of being able to do this and I guess I have a bit of coding to do to get it to work in the three directions for each node.  I was just wondering if there was a quicker solution. I will give it a try and see how I get on. Thanks as always. ---------------------------------------------------------------------- wp · 2024-07-21 10:47 Which graphics library do you use for drawing? ---------------------------------------------------------------------- ranny · 2024-07-21 10:54 Hmm... I'm not very good at identifying these things, I just installed the lazOpenGlContext package and use the basic OpenGl commands.  Lazarus is version 3.2 with FPC 3.2.2 if that makes a difference.... ---------------------------------------------------------------------- wp · 2024-07-21 11:18 This is OpenGL - simply add the colors to the OpenGL model of the lines, like I showed in the previous post. ---------------------------------------------------------------------- ranny · 2024-07-21 11:38 Sorry, I should have mentioned before, all graphics in this project are OpenGl.  It's taking me quite a while to get an understanding it but I am pleased with the results. For your suggestion I will find a way of linearly moving from, say, red to blue then compare to each orthogonally displaced point.  Will keep me busy for a wee while. Thanks. ---------------------------------------------------------------------- wp · 2024-07-21 12:03 In TAChart, there is a function for interpolating colors between two end colors weighted by a factor ACoeff (0..1): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function InterpolateRGB(AColor1, AColor2: Integer; ACoeff: Double): Integer;type  TBytes = packed array [1..4] of Byte;var  c1: TBytes absolute AColor1;  c2: TBytes absolute AColor2;  r: TBytes absolute Result;  i: Integer;begin  ACoeff := EnsureRange(ACoeff, 0.0, 1.0);  for i := 1 to 4 do    r[i] := Round(c1[i]  + (c2[i] - c1[i]) * ACoeff);end; Adapting to your project, I would say, that ACoeff is the relative displacement (displacement at a vertex / max displacement). So, when you want to draw a segment between vertices V1 and V2, calculate the interpolated colors for both vertices and add them to the OpenGL command: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const  MinDisplColor = clBlue;  MaxDisplColor = clRed;var  MaxDisplacement: Double; procedure DrawSegment(V1, V1: TPoint3d; Displ1, Displ2: Double);var  col1, col2: TColor;begin  col1 := InterpolateRGB(MinDisplColor, MaxDisplColor, Displ1/MaxDisplacement);  col2 := InterpolateRGB(MinDisplColor, MaxDisplColor, Displ2/MaxDisplacement);  glBegin(GL_LINES);    glColor3f(Red(col1)/255, Green(col1)/255, Blue(col1)/255);    glVertex3f(V1.X, V1.Y, V1.Z);    glColor3f(Red(col2)/255, Green(col2)/255, Blue(col2)/255);    glVertex3f(V2.X, V2.Y, V2.Z);  glEnd;end; ---------------------------------------------------------------------- ranny · 2024-07-22 05:36 That's exactly what I am looking for!  Yesterday I spent the time getting the maximum values for displacement from the array of adjoining nodes and achieved some progress with colour graduation. Going forward, your routine will be useable for displaying maximum stresses in the structure as well. Thanks again, I' on the road to getting one of my projects close to completion, and genuinely useful!