Attractor Scripts

## RhinoCommon Code

#### Single Point Attractor

Implemented in a Grasshopper VB.NET component.

##### Circle Radius

```
Private Sub RunScript(ByVal P As Point3d, ByVal A As List(Of Point3d), ByRef G As Object)
Dim circlelist As New List (Of Circle)
For i As Integer = 0 To A.Count - 1
Dim distance As Double
distance = P.DistanceTo(A(i))
Dim radius As Double = distance * 0.015
Dim mycircle As Circle
mycircle = New Circle(A(i), radius)
circlelist.Add(mycircle)
Next
G = circlelist
End Sub
```

##### Circle Ripple

```
Private Sub RunScript(ByVal P As Point3d, ByVal A As List(Of Point3d), ByRef G As Object)
Dim circlelist As New List (Of Circle)
For i As Integer = 0 To A.Count - 1
Dim distance As Double
distance = P.DistanceTo(A(i))
Dim radius As Double = 2 * Math.Sin(distance * 0.05)
Dim mycircle As Circle
mycircle = New Circle(A(i), radius)
circlelist.Add(mycircle)
Next
G = circlelist
End Sub
```

##### Surface Ripple

```
Private Sub RunScript(ByVal P As Point3d, ByVal U As Integer, ByVal V As Integer, ByVal D As Double, ByRef S As Object)
Dim pointgrid As New List (Of Point3d)
Dim mysurface As NurbsSurface
For i As Integer = 0 To U - 1
For j As Integer = 0 To V - 1
Dim x As Double = i * D
Dim y As Double = j * D
Dim loc As Point3d = New Point3d(x, y, 0)
Dim distance As Double
distance = P.DistanceTo(Loc)
Dim z As Double = 10 * Math.Sin(distance * 0.1)
Dim srfpt As Point3d = New Point3d(x, y, z)
pointgrid.Add(srfpt)
Next
Next
mysurface = NurbsSurface.CreateFromPoints(pointgrid, U, V, 3, 3)
S = mysurface
End Sub
```

#### Multi-Point Attractor

Implemented in a Grasshopper VB.NET component.

##### Multi-Point Circle Ripple

```
Private Sub RunScript(ByVal P As List(Of Point3d), ByVal U As Integer, ByVal V As Integer, ByVal D As Double, ByRef C As Object)
Dim circlelist As New List (Of Circle)
For i As Integer = 0 To U - 1
For j As Integer = 0 To V - 1
Dim x As Double = i * D
Dim y As Double = j * D
Dim loc As Point3d = New Point3d(x, y, 0)
Dim distance As Double = 0
Dim att As Double = 0
For k As Integer = 0 To P.Count - 1
distance = distance + P(k).DistanceTo(Loc)
att = att + (2 * Math.Sin(distance * .05))
Next
Dim mycircle As Circle = New Circle(loc, att)
circlelist.Add(mycircle)
Next
Next
C = circlelist
End Sub
```

##### Multi-Point Surface Ripple

```
Private Sub RunScript(ByVal P As List(Of Point3d), ByVal U As Integer, ByVal V As Integer, ByVal D As Double, ByRef S As Object)
Dim pointgrid As New List (Of Point3d)
Dim mysurface As NurbsSurface
For i As Integer = 0 To U - 1
For j As Integer = 0 To V - 1
Dim x As Double = i * D
Dim y As Double = j * D
Dim loc As Point3d = New Point3d(x, y, 0)
Dim distance As Double = 0
Dim att As Double = 0
For k As Integer = 0 To P.Count - 1
distance = distance + P(k).DistanceTo(Loc)
att = att + (10 * Math.Sin(distance * .1))
Next
Dim z As Double = att / p.Count
Dim srfpt As Point3d = New Point3d(x, y, z)
pointgrid.Add(srfpt)
Next
Next
mysurface = NurbsSurface.CreateFromPoints(pointgrid, U, V, 3, 3)
S = mysurface
End Sub
```

## Processing

##### Simple Attractor

import toxi.geom.*; int xcount; int ycount; Vec3D grid[][]; void setup() { size(800, 600, P3D); background(0); int incr = 40; xcount = width/incr; ycount = height/incr; grid = new Vec3D[xcount+1][ycount+1]; for (int i=0; i<=xcount;i++) { for (int j=0; j<=ycount;j++) { grid[i][j] = new Vec3D(i*incr, j*incr, 0); } } } void draw() { background(0); stroke(255); noFill(); Vec3D attpt = new Vec3D(mouseX, mouseY, 0); for (int i=0;i<=xcount;i++) { for (int j=0;j<=ycount;j++) { float distance = (attpt.distanceTo(grid[i][j]))*.05; ellipse(grid[i][j].x, grid[i][j].y, distance, distance); } } }

##### Single-Point Ripple

Requires the Toxiclibs library

import toxi.geom.*; int xcount; int ycount; Vec3D grid[][]; void setup() { size(800, 600, P3D); background(0); int incr = 10; xcount = width/incr; ycount = height/incr; grid = new Vec3D[xcount+1][ycount+1]; for (int i=0; i<=xcount;i++) { for (int j=0; j<=ycount;j++) { grid[i][j] = new Vec3D(i*incr, j*incr, 0); } } } void draw() { background(0); noStroke(); Vec3D attpt = new Vec3D(mouseX, mouseY, 0); for (int i=0;i<=xcount;i++) { for (int j=0;j<=ycount;j++) { float distance = (attpt.distanceTo(grid[i][j]))*.1; float attvar = 10*sin((frameCount*-.01)+(distance*.2)); fill(0,4*distance,255-attvar*30); ellipse(grid[i][j].x, grid[i][j].y, attvar, attvar); } } }

## License

The code and components on this page are free software: you can redistribute them and/or modify them under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.