# Curve Stitching

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.

• Oh yeah! D'oh... – James Williams Sep 12 '14 at 15:37
• 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 :-) – Level River St Sep 12 '14 at 16:07
• @Martin Yup, pixels. – James Williams Sep 12 '14 at 16:15
• @Martin The horizontal line shouldn't be necessary, because the border of the window will make up the lines. – James Williams Sep 12 '14 at 16:41
• Oh dayum I used to do this for fun when bored in class ... didn't know other people had done the exact same thing! – Claudiu Sep 12 '14 at 20:32

## Mathematica, 5551504764 68 bytes

f=Graphics[Line@Table[#2{{i-1,#+1},{0,i+1}},{i,#}],ImageSize->#*#2]&


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

f[25,15]


Yielding 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
n,p=input()
for i in range(n):pylab.plot([0,i*p],[(i-n)*p,0]) • 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) – Martin Ender Sep 12 '14 at 16:14
• (The question does specify units as pixels now, btw.) – Martin Ender Sep 12 '14 at 19:32

# Bash+Imagemagick+xview, 124 bytes

for((;i<$1;));{ 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: • Yay for overflowing the maximum command line length if you're not careful. Nice one – 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:

25x15: 6x72: # BBC Basic, 58 ascii characters, tokenised filesize 49

INPUTn,p:p*=2FORi=1TOn:MOVEi*p-p,974DRAW0,974-(n-i)*p:NEXT


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. • This is fast becoming a dangerous golfing language. CJam who? – 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>
p=prompt,s=p(l=p(t=c.getContext("2d")));for(c.width=c.height=y=s*l,x=0;l--;x-=s)t.moveTo(0,y-=s),t.lineTo(-x,0);t.stroke()
</script>


Fiddle First input number of lines, second input pixel interval

Ungolfed Fiddle

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