two-ticks / p5.teach.js

A beginner friendly math animation library for p5.js
https://two-ticks.github.io/p5.teach.js/
59 stars 7 forks source link

Arrow function #21

Open JithinKS97 opened 3 years ago

JithinKS97 commented 3 years ago

A function that accepts the same parameters as line and draws an arrow is very useful. In math and physics, there are a lot of places where this can come handy.

arrow(x1, y1, x2, y2) will be the function

nickmcintyre commented 3 years ago

@JithinKS97 how should we handle dimensions and color?

JithinKS97 commented 3 years ago

By dimension, I assume you are talking about 2d and 3d, I think we can handle in the same way it is dealt in line function. fill and stroke also, if fill and stroke is called before arrow, it should take those values.

In short, it should behave the same way as line but there should be a triangle that points at the end.

nickmcintyre commented 3 years ago

Nope, but the considerations for 2D and 3D are similar. Let's say a 2D arrow is composed of a rectangle and triangle. Should the dimensions of the triangular head depend upon the width and height of the rectangular tail? Should we treat arrows differently if they're part of a plot?

two-ticks commented 3 years ago

Can we use something like this ?

myArrowConfig = { 
      arrowTipHeight: 1, //px 
      arrowTipWidth:  2, 
      arrowTailWidth : 1
}
myArrow.configure(myArrowConfig)
myArrowStyle = { 
      arrowTipHeight: 1, //px 
      arrowTipWidth:  2, 
      arrowTailWidth : 1
}
myArrow.style(myArrowStyle)
two-ticks commented 3 years ago

branch : beginGraph

V1LVv4ijF4

🔗 web-editor sketch

arrow

beginGraph();
 tangent = arrow(-20, -100, 150, 100);
  tangent.configure({
    arrowSize: 3,
    arrowHeadColor: color(40, 140, 10, 150)
    // arrowHeadHeight : 4, this is currently not available
  });
endGraph();
tangent.arrowHead(20, 20);

controlPoint

beginGraph();
ctr1 = controlPoint(100, 100);
endGraph();

@JithinKS97 @nickmcintyre

JithinKS97 commented 3 years ago

Looks good @two-ticks

JithinKS97 commented 3 years ago

@two-ticks Its always good to have a default behaviour like

fill('red')
stroke('black')
arrow(0,0,50,50)

should produce a default arrow with fill and stroke

nickmcintyre commented 3 years ago

@two-ticks I've worked a bit more with arrow() and have a few observations based on making the figure below.

https://editor.p5js.org/mcintyre/sketches/hu-mrfhGt

two-ticks commented 3 years ago

The arrow size and head color problem are due to this.svgLine.setAttribute('marker-end', 'url(#marker-arrow)'), instead defs defined in current <g> arrow is taking url(#marker-arrow) from the oldest defined defs. I am working on this issue.

one more bug I have found related to SVGs

For co-ordinates I need some suggestions, here are some points I need help with

I want to unify all the coordinate system or find any middle ground for example a config({coordinateSystem: 'rightHanded'})

two-ticks commented 3 years ago

arrow has been now improved in #27

nickmcintyre commented 3 years ago

Good clarification - by "unify" I just mean to apply a single coordinate system at a time. Here's an example of how I tried to solve this problem on the canvas.

coordinateMode(RIGHT_HAND) // or LEFT_HAND

Also made a few small modifications to p5.prototype._updateNextMouseCoords and p5.prototype.text to get the basics working.

Re: plotting - Enabling users to set the coordinate system should help here.