8
\$\begingroup\$

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

Imgur

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

Imgur

n = 20, p = 20

Imgur


This is a challenge, so shortest code wins.

\$\endgroup\$
  • \$\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
5
\$\begingroup\$

Mathematica, 55 51 50 47 64 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

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.

| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

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])

enter image description here

| improve this answer | |
\$\endgroup\$
  • \$\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
3
\$\begingroup\$

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:

enter image description here

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

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:

25x15

6x72:

6x72

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

BBC Basic, 58 ascii characters, tokenised filesize 49

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

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

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

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

| improve this answer | |
\$\endgroup\$
  • \$\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.