Curve stitching is the process of "stitching" together multiple straight lines in order to create a curve, like so:


For an explanation of curve stitching, visit this website.

We will be drawing our curve in the top left of the screen, as shown in the image above.

Given an integer n (via STDIN or a function parameter), being the number of lines to draw, and an integer p pixels, being the interval between the starting points, draw a straight line curve.

Horizontal/vertical lines are necessary, and should be part of the line count.

Example Outputs:

n = 25, p = 15


n = 20, p = 20


This is a challenge, so shortest code wins.

  • \$\begingroup\$ Oh yeah! D'oh... \$\endgroup\$ – James Williams Sep 12 '14 at 15:37
  • \$\begingroup\$ The curve is in fact a parabola. I can't remember where I read that thoughl. If I was a pedant I would point out that it's not a "straight line curve" but an approximation of a curve built up from straight lines :-) \$\endgroup\$ – Level River St Sep 12 '14 at 16:07
  • \$\begingroup\$ @Martin Yup, pixels. \$\endgroup\$ – James Williams Sep 12 '14 at 16:15
  • \$\begingroup\$ @Martin The horizontal line shouldn't be necessary, because the border of the window will make up the lines. \$\endgroup\$ – James Williams Sep 12 '14 at 16:41
  • 2
    \$\begingroup\$ Oh dayum I used to do this for fun when bored in class ... didn't know other people had done the exact same thing! \$\endgroup\$ – Claudiu Sep 12 '14 at 20:32

Mathematica, 55 51 50 47 64 68 bytes


Defines a function which yields the image as specified when called like



enter image description here

Edit: Had to add some characters to make sure that the second parameter was actually interpreted as pixels.

Edit: four more bytes to plot the horizontal lines.


Python - 74

Since the question doesn't specify units, axes scaling etc. I'm coming up with the following minimum solution:

import pylab
for i in range(n):pylab.plot([0,i*p],[(i-n)*p,0])

enter image description here

  • \$\begingroup\$ Disregarding units and axis scaling, the minimum solution saves four characters by omitting *p ;). also, are you sure you're not adding a horizontal and/or vertical line? those are not in the example outputs. finally, I noticed in mine, that it's shorter to offset the first coordinate horizontally instead of the second one (because you can save the - and the parentheses) \$\endgroup\$ – Martin Ender Sep 12 '14 at 16:14
  • 2
    \$\begingroup\$ (The question does specify units as pixels now, btw.) \$\endgroup\$ – Martin Ender Sep 12 '14 at 19:32

Bash+Imagemagick+xview, 124 bytes

s+=" -draw 'line $[i*$2],0 0,$[($1-i++)*$2]'"
eval convert -size $[$1*$2]x$[$1*$2] xc:$s png:-|xview stdin

Output for ./curvestitch.sh 25 15:

enter image description here

  • \$\begingroup\$ Yay for overflowing the maximum command line length if you're not careful. Nice one \$\endgroup\$ – tomsmeding Sep 13 '14 at 22:11

Perl, 121 130 bytes

The input is via STDIN. The values are comma separated.

EDIT: We have new rules. I'm not sure why, but the first two pixels are invisible and I had to add an offset...

use Tk;<>=~/,/;$c=tkinit->Canvas(-width=>$w=$`*$'-$',-height=>$w)->pack;$c->createLine(2,2+$_*$',2+$w-$_*$',2)for 0..~-$`;MainLoop

Here are some tests:






BBC Basic, 58 ascii characters, tokenised filesize 49


Download emulator at http://www.bbcbasic.co.uk/bbcwin/bbcwin.html

p*=2 is needed because in the default mode BBC Basic maps a logical square of 2x2 to a single physical pixel.

BBC Basic has the origin at the bottom left corner of the screen, with y coorinates going up. On my machine the default window has an upper y coordinate of 974 (yours may be different.) 7 characters could be saved if it was permitted to plot in the bottom left corner of the screen. Adding MODE16 after the first : will resize the window so that the upper y coordinate is guaranteed to be 799.

enter image description here

  • \$\begingroup\$ This is fast becoming a dangerous golfing language. CJam who? \$\endgroup\$ – Soham Chowdhury Sep 13 '14 at 2:29

Html + JavaScript 155 157 183

Edit: learnig what stuff I can cut without functionality loss
Edit 2: as suggested by @Optimizer

<canvas id='c'/><script>

Fiddle First input number of lines, second input pixel interval

Ungolfed Fiddle

  • \$\begingroup\$ l=p(s=p(t=c.getContext("2d"))) \$\endgroup\$ – Optimizer Sep 13 '14 at 10:56
  • \$\begingroup\$ @Optimizer I think I need to set width and height before getting the context \$\endgroup\$ – edc65 Sep 13 '14 at 13:08
  • 1
    \$\begingroup\$ Nope, this works : jsfiddle.net/6ke43m7c/10 \$\endgroup\$ – Optimizer Sep 13 '14 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.