Attractor point to geometry



Hi I have a issue with the attractor input in my c# code. as in the first image the bricks are generated on the points and this is how it ought to look. the issue is when i introduce the attactor input to the component. the code is changing the brep geometry and not rotating the bricks by the factor remaped. is there soming i am overlooking?

using System;
using System.Collections;
using System.Collections.Generic;
using Rhino;
using Rhino.Geometry;
using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;

public abstract class Script_Instance_bf09d : GH_ScriptInstance
{
  #region Utility functions
  private void Print(string text) { /* Implementation hidden. */ }
  private void Print(string format, params object[] args) { /* Implementation hidden. */ }
  private void Reflect(object obj) { /* Implementation hidden. */ }
  private void Reflect(object obj, string method_name) { /* Implementation hidden. */ }
  #endregion

  #region Members
  private readonly RhinoDoc RhinoDocument;
  private readonly GH_Document GrasshopperDocument;
  private readonly IGH_Component Component;
  private readonly int Iteration;
  #endregion

  #region Runscript
  private void RunScript(List<Point3d> Points, List<Point3d> Attractors, double XSize, double YSize, double ZSize, ref object Bricks)
  {
    List<Rectangle3d> rectangles = new List<Rectangle3d>();

    foreach (Point3d point in Points)
    {
      // Calculate the corner points of the rectangle
      double halfXSize = XSize / 2.0;
      double halfYSize = YSize / 2.0;
      Point3d corner1 = new Point3d(point.X - halfXSize, point.Y - halfYSize, point.Z);
      Point3d corner2 = new Point3d(point.X + halfXSize, point.Y - halfYSize, point.Z);
      Point3d corner3 = new Point3d(point.X + halfXSize, point.Y + halfYSize, point.Z);
      Point3d corner4 = new Point3d(point.X - halfXSize, point.Y + halfYSize, point.Z);

      // Create the rectangle
      Plane plane = new Plane(point, Vector3d.ZAxis);
      Rectangle3d rectangle = new Rectangle3d(plane, XSize, YSize);
      rectangles.Add(rectangle);
    }

    List<Brep> breps = new List<Brep>();
    foreach (Rectangle3d rectangle in rectangles)
    {
      double height = ZSize;
      Surface surface = Surface.CreateExtrusion(rectangle.ToNurbsCurve(), Vector3d.ZAxis * height);
      Brep brep = Brep.CreateFromSurface(surface);
      breps.Add(brep);
    }

    // Calculate distances between points and attractors
    List<double> distances = new List<double>();
    foreach (Point3d point in Points)
    {
      double minDistance = double.MaxValue;
      foreach (Point3d attractor in Attractors)
      {
        double distance = point.DistanceTo(attractor);
        minDistance = Math.Min(minDistance, distance);
      }
      distances.Add(minDistance);
    }

    // Normalize distances
    double minDist = GetListMin(distances);
    double maxDist = GetListMax(distances);
    List<double> normalizedDistances = new List<double>();
    foreach (double dist in distances)
    {
      double normalizedDist = MapValue(dist, minDist, maxDist, 0, 1);
      normalizedDistances.Add(normalizedDist);
    }

    // Remap distances to rotation angles
    List<double> rotationAngles = new List<double>();
    foreach (double normalizedDist in normalizedDistances)
    {
      double rotationAngle = MapValue(normalizedDist, 0, 1, 0, Math.PI / 4);
      rotationAngles.Add(rotationAngle);
    }

    // Apply rotation angles to rectangles
    for (int i = 0; i < rectangles.Count; i++)
    {
      Rectangle3d rectangle = rectangles[i];
      double rotationAngle = rotationAngles[i];
      Plane plane = rectangle.Plane;

      // Rotate the plane of the rectangle around its origin
      plane.Rotate(rotationAngle, plane.ZAxis);

      // Create a new rotated rectangle using the rotated plane
      Rectangle3d rotatedRectangle = new Rectangle3d(plane, rectangle.Width, rectangle.Height);

      rectangles[i] = rotatedRectangle;
    }

    Bricks = breps;
  }
  #endregion

  #region Additional
  private double GetListMin(List<double> list)
  {
    double min = double.MaxValue;
    foreach (double value in list)
    {
      if (value < min)
        min = value;
    }
    return min;
  }

  private double GetListMax(List<double> list)
  {
    double max = double.MinValue;
    foreach (double value in list)
    {
      if (value > max)
        max = value;
    }
    return max;
  }

  private double MapValue(double value, double inputMin, double inputMax, double outputMin, double outputMax)
  {
    return outputMin + (outputMax - outputMin) * ((value - inputMin) / (inputMax - inputMin));
  }
  #endregion
}

@dale thank you for your reply. It did not work. Still the same result.

@Jacek4 - I didn’t ‘fix’ your code, just reformatted to others can read it…

ok thank you

See attached: get the gist of how it works and modify it accordingly in order to suit your goals (includes a not so simple challenge as well).

Attractors_OnRndFacePts_V1.gh (148.9 KB)