Skip to content

Linestyle parameter for line() : solid, dotted, dashed,... #3336

@ThatIsAPseudo

Description

@ThatIsAPseudo

Nature of issue?

  • Found a bug
  • Existing feature enhancement
  • New feature request

Most appropriate sub-area of p5.js?

  • Color
  • Core/Environment/Rendering
  • Data
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • Other (specify if possible)

Feature enhancement details:

(Sorry for the grammatical mistakes, i'm not English)

N.B. : This feature has already been requested here #3016 , however it has been put away because its implementation wouldn't fit p5.js' scope. So I'm suggesting another way to do it just be enhancing an existing feature.

It would be great to be able to specify what type of line (solid, dotted,...) the line() function draws. Indeed you may need to draw dotted lines, and the only way to do it is by drawing several small lines/points with gaps between them through a for loop.

However, even though a vertical/horizontal line is quite easy to handle, the only way to draw diagonal lines is by struggling with maths...

I think the line() function could have a fifth parameter, by default being "solid", "dot", etc, or maybe we could imagine a way similar to the plot function in Python from library matplotlib, i.e. "--", ".", etc.

EDIT

Here is a suggestion of a linedash() function using line(), suggests and improvements are welcome :

function linedash(x1, y1, x2, y2, delta, style = '-') {
  // delta is both the length of a dash, the distance between 2 dots/dashes, and the diameter of a round
  let distance = dist(x1,y1,x2,y2);
  let dashNumber = distance/delta;
  let xDelta = (x2-x1)/dashNumber;
  let yDelta = (y2-y1)/dashNumber;

  for (let i = 0; i < dashNumber; i+= 2) {
    let xi1 = i*xDelta + x1;
    let yi1 = i*yDelta + y1;
    let xi2 = (i+1)*xDelta + x1;
    let yi2 = (i+1)*yDelta + y1;

    if (style == '-') { line(xi1, yi1, xi2, yi2); }
    else if (style == '.') { point(xi1, yi1); }
    else if (style == 'o') { ellipse(xi1, yi1, delta/2); }
  }
}

Syntax

linedash(x1, y1, x2, y2, delta)
linedash(x1, y1, x2, y2, delta, style)

Parameters
x1 Number : the x-coordinate of the first point
y1 Number : the x-coordinate of the second point
x2 : Number : the x-coordinate of the first point
y2 : Number : the y-coordinate of the second point
delta Number : the length of (and between) 2 dashes/points
style String : '-' for a dashline, '.'for dots, 'o'for bigger dots (rounds)

Note that since this function uses the ellipse() function, it may be affected by fill().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions