Extend the line

Given an image with a line on it, produce or display an image with the line extended till the edge of image. The line is black and the background is white. The image size is 100x100 The image comes in any reasonable format (e.g. jpg, png, ppm, bmp).

Line format

I'm using a simplified version of Bresenham's line algorithm for drawing lines. Lines will only have an integer slope so that the line will never partially cover a pixel but not color it:

At a minimum the line will be 3x3 unless the line is straight, in which case you will only see 3x1 or 1x3 in the image. Lines will also have one side on the edge of the image, so you only have to extend it in one direction.

Real examples (.png)

====================

• Would the program have to take the image input as the filename or would it be allowed to take input in other forms? (e.g. hexdump, link, upload, copy/paste) I only ask because some programming languages would require the use of other forms of input. Feb 17, 2016 at 18:02
• I don't know of any languages that can't read a image file. Do you know a common format that I can include in the I/O section as valid? Feb 17, 2016 at 18:05
• @JAtkin can we take input as a URL? Feb 18, 2016 at 22:58
• I guess... Why would you want to? Feb 18, 2016 at 22:59

APL (dzaima/APL), 93 bytes

{w←⍵⋄m←÷/(⊢/-⊃)p←⍸¯1≠⍵.mat⋄y x←⊃p⋄P5.size←⍵.sz⋄P5.setup←{P5.G.bg¯1⋄P5.G.ln∊{⍵,y+m×⍵-x}¨⍳101}}


Try it online!

A dfn which takes the image as an APLImg object. Taking the image as a URL is 102: Try it online!

Uses ⎕IO←0 (0-indexing).

The function effectively takes the line, calculates its equation and redraws it.

Explanation

Slope calculation: m←÷/(⊢/-⊃)p←⍸¯1≠⍵.mat

⍵.mat get the image as an integer matrix

¯1≠ check which pixels aren't white

⍸ get all the required coordinates

p← store in p

(⊢/-⊃) subtract the last point from the first

÷/ reduce by division

y x←⊃p store the first point in y and x

P5.size←⍵.sz set canvas size to image size

Draw the image: P5.setup←{P5.G.bg¯1⋄P5.G.ln∊{⍵,y+m×⍵-x}¨0,w.width}

P5.setup←{...} draw the following once:

P5.G.bg¯1 set the background to white

P5.G.ln∊{⍵,y+m×⍵-x}¨⍳101 draw lines between the following points:

{...}¨0,w.width get the points for each x in 0-100:

y+m×⍵-x $$\y = y_1+m(x-x_{1})\$$

⍵, pair the x value with that

∊ enlist so it can be drawn

Mathematica, 125 bytes

ImageRotate@Image@SparseArray[Array[Floor,101,#&@@ImageLines[ColorNegate@#,Method->"RANSAC"]]+1->0,{101,101},1][[;;-2,;;-2]]&


Explanation

ImageLines detect lines in the image with method RANSAC. We take the first detected line and convert it back to an image. The whole function takes an image as the argument and returns an image.