How to draw a line between the two points?

3,239 views
Skip to first unread message

kawamoto

unread,
Jul 10, 2013, 12:49:21 AM7/10/13
to golan...@googlegroups.com
Hi,

Please teach me how to draw a line between the two points on a image.

I searched on internet, but I couldn't find it.

Regard,
Kawamoto

Nguyên Nguyễn Văn Cao

unread,
Jul 10, 2013, 12:57:02 AM7/10/13
to golan...@googlegroups.com
You must find the linear equations base on two point.
For example you have M (1, 2), N (2, -1).
Then you have: y = -3x + 5
And then a for loop (which do x++) to point to every single point or somethign similar.


Vào 11:49:21 UTC+7 Thứ tư, ngày 10 tháng bảy năm 2013, kawamoto đã viết:

Nguyên Nguyễn Văn Cao

unread,
Jul 10, 2013, 1:01:41 AM7/10/13
to golan...@googlegroups.com
But must be a third-partty pkg for that..


Vào 11:49:21 UTC+7 Thứ tư, ngày 10 tháng bảy năm 2013, kawamoto đã viết:
Hi,

Nigel Tao

unread,
Jul 10, 2013, 1:18:23 AM7/10/13
to kawamoto, golang-nuts
On Wed, Jul 10, 2013 at 2:49 PM, kawamoto <kwm...@gmail.com> wrote:
> Please teach me how to draw a line between the two points on a image.

A simple algorithm is

----
package main

import (
"code.google.com/p/go-tour/pic"
"image"
"image/color"
)

func main() {
m := image.NewRGBA(image.Rect(0, 0, 100, 100))
for x := 20; x < 80; x++ {
y := x/3 + 15
m.Set(x, y, color.Black)
}
pic.ShowImage(m)
}
----

Paste that into http://tour.golang.org/#59 to try it.

Yasutaka Kawamoto

unread,
Jul 10, 2013, 3:42:11 AM7/10/13
to Nigel Tao, golang-nuts
Thank you All.

I understand.

Do you know the function like
http://effbot.org/imagingbook/imagedraw.htm#tag-ImageDraw.Draw.line of
python?

Dan Kortschak

unread,
Jul 10, 2013, 5:10:46 AM7/10/13
to Yasutaka Kawamoto, Nigel Tao, golang-nuts
Ethan Burns' vector graphics interface package code.google.com/p/plotinum/vg will give you something like that with:

p = append(vg.Path(nil), xy...)
c.Stroke(p)

Mateusz Czapliński

unread,
Jul 10, 2013, 5:33:36 AM7/10/13
to golan...@googlegroups.com
On Wednesday, July 10, 2013 6:49:21 AM UTC+2, kawamoto wrote:
Please teach me how to draw a line between the two points on a image.

Ethan Burns

unread,
Jul 10, 2013, 7:59:24 AM7/10/13
to golan...@googlegroups.com
You should be able to modify this to take a regular image.Image instead of one wrapped in an ImageCanvas type: https://github.com/eaburns/quart/blob/master/geom/2d_draw.go#L164.  Also, checkout http://en.wikipedia.org/wiki/Bresenham's_line_algorithm.


Ethan

Ethan Burns

unread,
Jul 10, 2013, 8:00:43 AM7/10/13
to golan...@googlegroups.com
Oops, Mateusz already linked it :-D

Enjoy

Yasutaka Kawamoto

unread,
Jul 10, 2013, 8:51:13 PM7/10/13
to Mateusz Czapliński, golang-nuts
Thank you All.
I could do the thing I want to do.
It is below.
--------
package main

import (
"github.com/akavel/polyclip-go"
"github.com/akavel/polyclip-go/polyutil"
"image"
"image/color"
"image/draw"
"image/png"
"os"
)

var list []float64 = []float64{x0, y0, x1, y1,......}

func main() {

m := image.NewRGBA(image.Rect(0, 0, 500, 500))
draw.Draw(m, m.Bounds(), &image.Uniform{color.RGBA{0, 0, 255,
255}}, image.ZP, draw.Src)

pts := list2points(list)
polyutil.DrawPolyline(pts, brush(m))

w, _ := os.Create("new.png")
defer w.Close()
png.Encode(w, m)
}

func brush(im draw.Image) func(x, y int) {
return func(x, y int) {
im.Set(x, y, color.RGBA{255, 255, 255, 255})
}
}

func list2points(l []float64) []polyclip.Point {
pts := make([]polyclip.Point, 0)
for i := 0; i < len(l); i = i + 2 {
p := polyclip.Point{l[i], l[i+1]}
pts = append(pts, p)
}
return pts
}
--------

2013/7/10 Mateusz Czapliński <czap...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Tor Langballe

unread,
Jul 12, 2013, 2:40:57 PM7/12/13
to golan...@googlegroups.com
When I was a kid I made a routine to draw a line using only addition and subtraction, something like this:

func drawLine(sx, sy, ex, ey int) {
w := Abs(ex - sx)
h := Abs(ey - sy)
sum := w / 2               // this makes the line spread out better
y := sy
xinc = Sign(ex - sx) 
yinc = Sign(ey - sy) 
for x = sx; x != ex; x += inc {
drawPixel(x, y)
sum += h
if sum > w {
y += yinc
sum -= w
}
}
}

This case is for width > height only.

If you think about it, it is kind of doing division.

Reply all
Reply to author
Forward
0 new messages