From 9be1e1d07c8e4fceff5558e6d6c62573d9566cf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Christian=20Gr=C3=BCnhage?= Date: Fri, 26 May 2017 15:33:18 +0200 Subject: [PATCH] Initial Commit --- .idea/libraries/GOPATH__pixclient_.xml | 13 + .idea/libraries/Go_SDK.xml | 10 + .idea/modules.xml | 8 + .idea/pixclient.iml | 11 + .idea/vcs.xml | 6 + .idea/workspace.xml | 481 +++++++++++++++++++++++++ markov/markov.go | 68 ++++ pixclient.go | 50 +++ 8 files changed, 647 insertions(+) create mode 100644 .idea/libraries/GOPATH__pixclient_.xml create mode 100644 .idea/libraries/Go_SDK.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/pixclient.iml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 markov/markov.go create mode 100644 pixclient.go diff --git a/.idea/libraries/GOPATH__pixclient_.xml b/.idea/libraries/GOPATH__pixclient_.xml new file mode 100644 index 0000000..ca48328 --- /dev/null +++ b/.idea/libraries/GOPATH__pixclient_.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Go_SDK.xml b/.idea/libraries/Go_SDK.xml new file mode 100644 index 0000000..4a6b401 --- /dev/null +++ b/.idea/libraries/Go_SDK.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..07c23c6 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/pixclient.iml b/.idea/pixclient.iml new file mode 100644 index 0000000..632864b --- /dev/null +++ b/.idea/pixclient.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..f170070 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,481 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Client.connection.Write([]byte( + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + project + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/markov/markov.go b/markov/markov.go new file mode 100644 index 0000000..e8f4b5e --- /dev/null +++ b/markov/markov.go @@ -0,0 +1,68 @@ +package main + +import ( + "math/rand" + "git.jcg.re/jcgruenhage/pixclient" + "net" +) + +func main() { + x, y := 1920, 1200 + client := pixclient.Init(net.IPv4(94, 45, 231, 39), 1234) + currentX := rand.Intn(x) + currentY := rand.Intn(y) + r, g, b := byte(255), byte(0), byte(0) + counter := 0 + for true { + direction := rand.Intn(4) + for ((currentX == x-1) && (direction == 1)) || ((currentX == 0) && (direction == 3)) || ((currentY == y-1) && (direction == 2)) || ((currentY == 0) && (direction == 0)) { + direction = rand.Intn(4) + } + if direction == 0 { + currentY-- + } else if direction == 1 { + currentX++ + } else if direction == 2 { + currentY++ + } else if direction == 3 { + currentX-- + } else { + panic("Illegal Direction!") + } + if counter%500 == 0 { + iterateColor(&r, &g, &b) + } + client.WritePixel(currentX, currentY, r, g, b, 255) + counter++ + } +} +func iterateColor(r, g, b *byte) { + if *r == 255 { + if *b != 0 { + *b-- + } else if *g != 255 { + *g++ + } else { + *r-- + } + } else if *g == 255 { + if *r != 0 { + *r-- + } else if *b != 255 { + *b++ + } else { + *g-- + } + } else if *b == 255 { + if *g != 0 { + *g-- + } else if *r != 255 { + *r++ + } else { + *b-- + + } + } else { + panic("Illegal state") + } +} diff --git a/pixclient.go b/pixclient.go new file mode 100644 index 0000000..84bedd0 --- /dev/null +++ b/pixclient.go @@ -0,0 +1,50 @@ +package pixclient + +import ( + "net" + "strconv" +) + +type Client struct { + connection net.Conn +} + +func Init(addr net.IP, port int) *Client { + var client Client + connection, err := net.Dial("tcp", addr.String()+":"+strconv.Itoa(port)) + client.connection = connection + check(err) + return &client +} + +func (Client *Client) WritePixel(x, y int, r, g, b, a byte) { + var bytes []byte + bytes = append(appendTwoDigitHex(appendTwoDigitHex(appendTwoDigitHex(appendTwoDigitHex(append(append(append(append(append( + bytes, []byte("PX ")...), + []byte(strconv.FormatInt(int64(x), 10))...), + []byte(" ")...), + []byte(strconv.FormatInt(int64(y), 10))...), + []byte(" ")...), + r), + g), + b), + a), + []byte("\n")...) + Client.connection.Write(bytes) + + // + //Client.connection.Write([]byte( + // fmt.Sprintf("PX %d %d %X\n", x, y, []byte{r, g, b, a}))) +} +func appendTwoDigitHex(bytes []byte, val byte) []byte { + if val < 0x10 { + bytes = append(bytes, []byte("0")...) + } + return strconv.AppendInt(bytes, int64(val), 16) +} + +func check(e error) { + if e != nil { + panic(e) + } +}