# Pattern Attractors

**URL:** <https://discourse.processing.org/t/pattern-attractors/2535>\
**Category:** Project Guidance\
**Created:** [August 10, 2018, 4:13pm UTC](https://discourse.processing.org/t/pattern-attractors/2535 "2018-08-10T16:13:02Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![behreajj](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@behreajj](https://discourse.processing.org/u/behreajj)\
**Post date:** [August 13, 2018, 10:30am UTC](https://discourse.processing.org/t/pattern-attractors/2535/5 "2018-08-13T10:30:25Z")

</div>

Hi jakejake,

This looked neat, so I gave it a go here.

> **[Curve Attractor - OpenProcessing](https://www.openprocessing.org/sketch/579648)**
>
> Based on curve attractors in Grasshopper.

I know nothing about Grasshopper, but this [Generative Landscapes tutorial](https://generativelandscapes.wordpress.com/2014/08/14/curve-attractors-example-2-3/) laid out the process fairly well. Between that and the steps @jb4x laid out, I had a path forward. To repeat and rephrase what’s already been said:

- I took an ideal grid of the range ([0 - 1], [0 - 1]).

- I subdivided that range by a count, storing the result in an array of vectors.

- I used a sine function to generated an array of vectors.

- How did I know which distance from the cell to the curve is smallest? Here’s why the sketch is slow, and where there may be room to improve.

- The distance info needs to be compared against a maximum possible distance to be converted to a range [0 - 1]. Once that’s through, I can supply that range to `lerp` or `lerpColor`, etc.

Sorting an array by a custom criterion requires a bit more gear in Java, but the idea remains the same.

```auto
import java.util.Comparator;
import java.util.Arrays;

static void drawEllipses(PGraphics r, 
  PVector[] grid, PVector[] curve, 
  float left, float right, 
  float bottom, float top, 
  float padding, float minScale, 
  color clr0, color clr1, int clrMode, 
  DistSort sorter) {
  /* ... */
  for (int i = 0; i < len; ++i) {
    /* ... */
    Arrays.sort(curve, sorter);
    /* ... */
  }
  /* ... */
}

static class DistSort implements Comparator < PVector > {
  /* ... */
  int compare(PVector a, PVector b) {
     /* Return either -1, 0, or 1. */
  }
}

```

Hope that helps out. Best.

---

_[View the full topic](https://discourse.processing.org/t/pattern-attractors/2535)._
