8
\$\begingroup\$

Task

Given an image with a line on it, produce or display an image with the line extended the the line to 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:

(Wikipedia image)

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.

Example (.png, red line added so you can see it):

enter image description here enter image description here

Real examples (.png)

enter image description here enter image description here

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

enter image description here enter image description here

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

enter image description here enter image description here

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

enter image description here enter image description here

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

enter image description here enter image description here

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – DanTheMan Feb 17 '16 at 18:02
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ – J Atkin Feb 17 '16 at 18:05
  • \$\begingroup\$ @JAtkin can we take input as a URL? \$\endgroup\$ – Downgoat Feb 18 '16 at 22:58
  • \$\begingroup\$ I guess... Why would you want to? \$\endgroup\$ – J Atkin Feb 18 '16 at 22:59
2
\$\begingroup\$

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.

\$\endgroup\$

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.