123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- // Copyright 2015 by caixw, All rights reserved
- // Use of this source code is governed by a MIT
- // license that can be found in the LICENSE file.
- package identicon
- import (
- "image"
- "sync"
- )
- var pool = sync.Pool{
- New: func() interface{} { return make([]float64, 0, 10) },
- }
- var (
- // 可以出现在中间的方块,一般为了美观,都是对称图像。
- centerBlocks = []blockFunc{b0, b1, b2, b3}
- // 所有方块
- blocks = []blockFunc{b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16}
- )
- // 所有block函数的类型
- type blockFunc func(img *image.Paletted, x, y, size float64, angle int)
- // 将多边形points旋转angle个角度,然后输出到img上,起点为x,y坐标
- func drawBlock(img *image.Paletted, x, y, size float64, angle int, points []float64) {
- if angle > 0 { // 0角度不需要转换
- // 中心坐标与x,y的距离,方便下面指定中心坐标(x+m,y+m),
- // 0.5的偏移值不能少,否则坐靠右,非正中央
- m := size/2 - 0.5
- rotate(points, x+m, y+m, angle)
- }
- for i := x; i < x+size; i++ {
- for j := y; j < y+size; j++ {
- if pointInPolygon(i, j, points) {
- img.SetColorIndex(int(i), int(j), 1)
- }
- }
- }
- }
- // 全空白
- //
- // --------
- // | |
- // | |
- // | |
- // --------
- func b0(img *image.Paletted, x, y, size float64, angle int) {
- }
- // 全填充正方形
- //
- // --------
- // |######|
- // |######|
- // |######|
- // --------
- func b1(img *image.Paletted, x, y, size float64, angle int) {
- isize := int(size)
- ix := int(x)
- iy := int(y)
- for i := ix + 1; i < ix+isize; i++ {
- for j := iy + 1; j < iy+isize; j++ {
- img.SetColorIndex(i, j, 1)
- }
- }
- }
- // 中间小方块
- // ----------
- // | |
- // | #### |
- // | #### |
- // | |
- // ----------
- func b2(img *image.Paletted, x, y, size float64, angle int) {
- l := size / 4
- x = x + l
- y = y + l
- for i := x; i < x+2*l; i++ {
- for j := y; j < y+2*l; j++ {
- img.SetColorIndex(int(i), int(j), 1)
- }
- }
- }
- // 菱形
- //
- // ---------
- // | # |
- // | ### |
- // | ##### |
- // |#######|
- // | ##### |
- // | ### |
- // | # |
- // ---------
- func b3(img *image.Paletted, x, y, size float64, angle int) {
- m := size / 2
- points := pool.Get().([]float64)[:0]
- drawBlock(img, x, y, size, 0, append(points,
- x+m, y,
- x+size, y+m,
- x+m, y+size,
- x, y+m,
- x+m, y,
- ))
- pool.Put(points)
- }
- // b4
- //
- // -------
- // |#####|
- // |#### |
- // |### |
- // |## |
- // |# |
- // |------
- func b4(img *image.Paletted, x, y, size float64, angle int) {
- points := pool.Get().([]float64)[:0]
- drawBlock(img, x, y, size, angle, append(points,
- x, y,
- x+size, y,
- x, y+size,
- x, y,
- ))
- pool.Put(points)
- }
- // b5
- //
- // ---------
- // | # |
- // | ### |
- // | ##### |
- // |#######|
- func b5(img *image.Paletted, x, y, size float64, angle int) {
- points := pool.Get().([]float64)[:0]
- m := size / 2
- drawBlock(img, x, y, size, angle, append(points,
- x+m, y,
- x+size,
- y+size,
- x, y+size,
- x+m, y,
- ))
- pool.Put(points)
- }
- // b6 矩形
- //
- // --------
- // |### |
- // |### |
- // |### |
- // --------
- func b6(img *image.Paletted, x, y, size float64, angle int) {
- points := pool.Get().([]float64)[:0]
- m := size / 2
- drawBlock(img, x, y, size, angle, append(points,
- x, y,
- x+m, y,
- x+m, y+size,
- x, y+size,
- x, y,
- ))
- pool.Put(points)
- }
- // b7 斜放的锥形
- //
- // ---------
- // | # |
- // | ## |
- // | #####|
- // | ####|
- // |--------
- func b7(img *image.Paletted, x, y, size float64, angle int) {
- points := pool.Get().([]float64)[:0]
- m := size / 2
- drawBlock(img, x, y, size, angle, append(points,
- x, y,
- x+size, y+m,
- x+size, y+size,
- x+m, y+size,
- x, y,
- ))
- pool.Put(points)
- }
- // b8 三个堆叠的三角形
- //
- // -----------
- // | # |
- // | ### |
- // | ##### |
- // | # # |
- // | ### ### |
- // |#########|
- // -----------
- func b8(img *image.Paletted, x, y, size float64, angle int) {
- points := pool.Get().([]float64)[:0]
- m := size / 2
- mm := m / 2
- // 顶部三角形
- drawBlock(img, x, y, size, angle, append(points,
- x+m, y,
- x+3*mm, y+m,
- x+mm, y+m,
- x+m, y,
- ))
- // 底下左边
- drawBlock(img, x, y, size, angle, append(points[:0],
- x+mm, y+m,
- x+m, y+size,
- x, y+size,
- x+mm, y+m,
- ))
- // 底下右边
- drawBlock(img, x, y, size, angle, append(points[:0],
- x+3*mm, y+m,
- x+size, y+size,
- x+m, y+size,
- x+3*mm, y+m,
- ))
- pool.Put(points)
- }
- // b9 斜靠的三角形
- //
- // ---------
- // |# |
- // | #### |
- // | #####|
- // | #### |
- // | # |
- // ---------
- func b9(img *image.Paletted, x, y, size float64, angle int) {
- points := pool.Get().([]float64)[:0]
- m := size / 2
- drawBlock(img, x, y, size, angle, append(points,
- x, y,
- x+size, y+m,
- x+m, y+size,
- x, y,
- ))
- pool.Put(points)
- }
- // b10
- //
- // ----------
- // | ####|
- // | ### |
- // | ## |
- // | # |
- // |#### |
- // |### |
- // |## |
- // |# |
- // ----------
- func b10(img *image.Paletted, x, y, size float64, angle int) {
- points := pool.Get().([]float64)[:0]
- m := size / 2
- drawBlock(img, x, y, size, angle, append(points,
- x+m, y,
- x+size, y,
- x+m, y+m,
- x+m, y,
- ))
- drawBlock(img, x, y, size, angle, append(points[:0],
- x, y+m,
- x+m, y+m,
- x, y+size,
- x, y+m,
- ))
- pool.Put(points)
- }
- // b11 左上角1/4大小的方块
- //
- // ----------
- // |#### |
- // |#### |
- // |#### |
- // | |
- // | |
- // ----------
- func b11(img *image.Paletted, x, y, size float64, angle int) {
- points := pool.Get().([]float64)[:0]
- m := size / 2
- drawBlock(img, x, y, size, angle, append(points,
- x, y,
- x+m, y,
- x+m, y+m,
- x, y+m,
- x, y,
- ))
- pool.Put(points)
- }
- // b12
- //
- // -----------
- // | |
- // | |
- // |#########|
- // | ##### |
- // | # |
- // -----------
- func b12(img *image.Paletted, x, y, size float64, angle int) {
- points := pool.Get().([]float64)[:0]
- m := size / 2
- drawBlock(img, x, y, size, angle, append(points,
- x, y+m,
- x+size, y+m,
- x+m, y+size,
- x, y+m,
- ))
- pool.Put(points)
- }
- // b13
- //
- // -----------
- // | |
- // | |
- // | # |
- // | ##### |
- // |#########|
- // -----------
- func b13(img *image.Paletted, x, y, size float64, angle int) {
- points := pool.Get().([]float64)[:0]
- m := size / 2
- drawBlock(img, x, y, size, angle, append(points,
- x+m, y+m,
- x+size, y+size,
- x, y+size,
- x+m, y+m,
- ))
- pool.Put(points)
- }
- // b14
- //
- // ---------
- // | # |
- // | ### |
- // |#### |
- // | |
- // | |
- // ---------
- func b14(img *image.Paletted, x, y, size float64, angle int) {
- points := pool.Get().([]float64)[:0]
- m := size / 2
- drawBlock(img, x, y, size, angle, append(points,
- x+m, y,
- x+m, y+m,
- x, y+m,
- x+m, y,
- ))
- pool.Put(points)
- }
- // b15
- //
- // ----------
- // |##### |
- // |### |
- // |# |
- // | |
- // | |
- // ----------
- func b15(img *image.Paletted, x, y, size float64, angle int) {
- points := pool.Get().([]float64)[:0]
- m := size / 2
- drawBlock(img, x, y, size, angle, append(points,
- x, y,
- x+m, y,
- x, y+m,
- x, y,
- ))
- pool.Put(points)
- }
- // b16
- //
- // ---------
- // | # |
- // | ##### |
- // |#######|
- // | # |
- // | ##### |
- // |#######|
- // ---------
- func b16(img *image.Paletted, x, y, size float64, angle int) {
- points := pool.Get().([]float64)[:0]
- m := size / 2
- drawBlock(img, x, y, size, angle, append(points,
- x+m, y,
- x+size, y+m,
- x, y+m,
- x+m, y,
- ))
- drawBlock(img, x, y, size, angle, append(points[:0],
- x+m, y+m,
- x+size, y+size,
- x, y+size,
- x+m, y+m,
- ))
- pool.Put(points)
- }
|