canvas-sketch-util icon indicating copy to clipboard operation
canvas-sketch-util copied to clipboard

[Feature] Random point on Triangle

Open TheMapSmith opened this issue 6 years ago • 3 comments

👋 Hey @mattdesl. I've been doing some sketches with triangles recently and I might have some building blocks that would allow for a new random.onTriangle() feature that will randomly choose n number of random points that fall on the side(s) of the triangle.

given: a triangle with vertices A, B, and C similar to below.

           A
           /\
          /  \
         /    \
        /      \
       /        \
      /          \
     /            \
    /              \
B  /________________\  C

which is made of an array of three coordinate pairs

const triangle = [[xa,ya],[xb,yb],[xc,yc]]

or maybe an object?

const triangle = {
  pointA: {
    xa: 1,
    ya: 0
  },
  pointB: {
    xb: 0,
    yb: 1
  },
  pointC: {
    xc: 1,
    yc: 1
  }
};

choose n number of random points that fall on the side(s) of the triangle.

command:

[x, y] = random.onTriangle(triangle as object or array, n as number, s as int for side number)

valid arguments: required
triangle object (as above)

optional
number of points to return
default = 1

optional
which "side" of the triangle
default = 0 side options:

  • 0 = any segment
  • 1 = segment between point A and B
  • 2 = segment between point B and C
  • 3 = segment between point A and C

future enhancement? 12 as valid argument (side 1 and side 2)

Does this sound like something you'd like to see in the library? If so, I'd love to help. (but I might need a little help helping 😕)

TheMapSmith avatar Jan 03 '19 22:01 TheMapSmith

Hey! Thanks for the issue request. I do agree this would be useful, I would say something like:

random.insideTriangle(a, b, c, out = [], u = random.value(), v = random.value());

I already have an implementation I am using in my local code:

https://gist.github.com/mattdesl/4337f1ad890d3fc713e1b42dbb1603ff

I think adding insideTriangle should also probably have onTriangle (i.e. on the perimeter of the triangle) for parity with circle/sphere, but finding a random point around a triangle edge may be a little trickier (i.e. weighted random based on line segment lengths?).

mattdesl avatar Jan 03 '19 23:01 mattdesl

Oops, I misread your comment! It seems like you want points on the edge not inside the triangle. But yes maybe both variations would be good to have!

mattdesl avatar Jan 03 '19 23:01 mattdesl

Matt,

Yes, I was thinking that in and on triangle would be complementary code 😄

I looked at your current implementation. It's very concise! That would have taken me probably 50 lines at least 😐

Do I understand it correctly?

  • Pass in 3 coordinate pairs a, b, c
  • Generate 2 random numbers < 1.0 u, v
  • Check that they aren't too big
  • Create a 3rd random factor Q by subtracting u, v from 1.0
  • Apply the random factors to each member of each coordinate pair
  • Return a single [x,y] pair

That's a clever way of doing it!

I have some working code that calculates the slope-intercept y=mx+b formula for the side of the triangle, generates a random X coordinate, and then calculates which Y coordinate intersects at that X. I think I could translate that into a generic module if you want.

TheMapSmith avatar Jan 04 '19 04:01 TheMapSmith