Code Monkey home page Code Monkey logo

Comments (3)

mattdesl avatar mattdesl commented on August 29, 2024

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?).

from canvas-sketch-util.

mattdesl avatar mattdesl commented on August 29, 2024

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!

from canvas-sketch-util.

TheMapSmith avatar TheMapSmith commented on August 29, 2024

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.

from canvas-sketch-util.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.