Straight Line to Text

Input:

2 numbers, x and y.

Output:

Output a text block where x is the width of the text block and y is the height of the text block. Then there will be a straight line going from the top left to the bottom right and to show it, there should be a # where the line should cross.

The # should be generated using the straight line equation, y = mx + c where m is the gradient of the line which can be retrieved through making m as y divided by x. x is the current line index. c is the height intercept and can be found by multiplying the gradient by the x value and adding the y value. So, in short, it would be (y/x) * lineIndex + (y/x * x + h). This would all be rounded to the nearest position in the current line.

Rules:

• there can only be 1 # per line
• it has to be in the form of a string/whatever the equivalent in your language of choice. This has to be able to cover multiple lines as well.
• in text block, after the # there does not need to be more spaces. Just initial ones to offset the #

Examples:

(don't mind the |, they are where the lines start and stop)

Where x is 9 and y is 1:

|        #|

Where x is 1 and y is 5:

|#|
|#|
|#|
|#|
|#|

where x is 9 and y is 5:

| #       |
|   #     |
|    #    |
|      #  |
|        #|

where x is 5 and y is 9:

|#    |
|#    |
| #   |
| #   |
|  #  |
|  #  |
|   # |
|   # |
|    #|

where x is 9 and y is 2:

|   #     |
|        #|

where x is 9 and y is 3:

|  #      |
|     #   |
|        #|

Edit: More detail in the output. Added 3rd rule. Added 5th and 6th example

Edit2: Fixed errors in the given examples (Sorry!). Revised over the output to make it much clearer v. sorry for any confusion :S

• How exactly is the position of # per line determined? How would the output for 9 and 2 look? Apr 2 '17 at 20:30
• Editted the main post but the position of the # should be determined by using the straight line equation, y = mx + c. you can find the examples for both 9, 2 and 9, 3 in the post Apr 2 '17 at 20:40
• You have four examples with x=9, and the first one (with y=1) is incompatible with the last three (and I'm not convinced that the second one, with y=5, is compatible with the last two). The instruction "The straight line should use the straight line equation, y = mx + c" is not sufficiently precise. Apr 2 '17 at 21:12
• very sorry about all the confusion, hopefully made it clearer now but do let me know if you find anything unclear so i can make it easy to understand. Apr 2 '17 at 21:41
• Can we choose to output more than 1 # per line if it helps save bytes? Apr 10 '17 at 13:24

C, 74 70 bytes

Thanks to @Johan du Toit for saving 4 bytes!

i;f(x,y){for(i=y;i--;)printf("%*c\n",(int)round((i-y)/(-1.*y/x)),35);}

Try it online!

• Nice! You can save a couple of bytes by using -1.*y/x instead of -y/(float)x
– jdt
Apr 11 '17 at 10:50

PHP, 66 Bytes

for([,$x,$y]=$argv;$i<$y;)printf("%".round(++$i*$x/$y)."s\n","#");
• 1. no need to init $i. 2. save 1 byte (and fix) with printf("%".round(++$i*$x/$y)."s\n","#"); Apr 10 '17 at 11:33

JavaScript (ES6), 77 bytes

x=>y=>[...Array(y).keys()].map(a=>" ".repeat(0|((a+1)/(y/x))+.5)+"#").join

f=x=>y=>[...Array(y).keys()].map(a=>" ".repeat(0|((a+1)/(y/x))+.5)+"#").join

console.log(f(9)(5));

Python, 64 63 bytes

1 byte saved thanks to @Cole

def f(x,y):[print(' '*round((i+1)*x/y-1)+'#')for i in range(y)]

Try it online!

• Using a list comprehension is 1 byte shorter (tested in Python 3.5.2): def f(x,y):[print(' '*round((i+1)*x/y-1)+'#')for i in range(y)]
– cole
Apr 10 '17 at 14:26
• @Cole am I allowed to return garbage (Nones)? (I thought about yielding the lines, but the output rules seemed to be strictly stdout here) Apr 10 '17 at 14:28
• Does a function implicitly return the result of the list comprehension? I'm pretty sure if you saved it in a file and ran it (which afaik is the standard way; you'd specify if it had to be done in a REPL) it would only print out the desired output. If I'm wrong then by all means ignore my suggestion.
– cole
Apr 10 '17 at 14:46
• @Cole Sorry, my bad. I guess I was confusing it with lambdas. Apr 10 '17 at 14:49

Japt, 21 bytes

ÆSpºX+1 /(U/V  r)+"#

Try it online!

Input is in the format of y x. +1 byte for the -R flag.

AWK, 55 44 bytes

{for(;++j<=$2;)printf"%*c\n",$1/$2*j+0.5,35} Using hints from @manatwork and the solution from @Steadybox The previous code is handy as an example of using an sprintf within a printf {for(;++j<=$2;)printf sprintf("%%%.0fs\n",$1/$2*j),"#"}

Example usage:

awk '{for(;++j<=$2;)printf"%*c\n",$1/$2*j+0.5,35}' <<< "9 3" • Not verified all test cases, but wouldn't {for(;++j<=$2;)printf"%*s\n",$1/$2*j+.5,"#"} do it? Apr 10 '17 at 19:30
• What case did it fail on? And I've never tried using the * with printf in AWK Apr 10 '17 at 19:36
• @manatwork It's good to learn new things :) Apr 10 '17 at 19:45