Problem of the Day
A new programming or logic puzzle every Mon-Fri

Line Drawing

Sometimes it would be great to be a kid again. To get back in that spirit let's start the day off by taking your favorite programming language and drawing out a line. The line must be at least 10px in length and must be output in to a standard image format (jpg, gif, png, svg, etc).

Permalink: http://problemotd.com/problem/line-drawing/

Comments:

  • James - 10 years, 4 months ago

    In Go (golang):

    package main
    
    import "os"
    import "image"
    import "image/draw"
    import "image/jpeg"
    
    func main() {
        // Create image in memory.
        dst := image.NewRGBA(image.Rect(0, 0, 500, 500))
    
        // Draw background.
        draw.Draw(dst, dst.Bounds(), &image.Uniform{image.White}, image.ZP, draw.Src)
    
        // Draw line.
        draw.Draw(dst, image.Rect(50, 245, 450, 255), &image.Uniform{image.Black}, image.ZP, draw.Src)
    
        // Create image file on disc.
        out, err := os.Create("line.jpg")
        if err != nil {
            panic(err.Error())
        }
        defer out.Close()
    
        // Write image to disc.
        jpeg.Encode(out, dst, nil)
    }
    

    reply permalink

Content curated by @MaxBurstein