Better builtin avatar generator (#17707)

This PR fixes the builtin avatar generator.

1. The random background color makes some images very dirty. So now we only use white background for avatars.
2. We use left-right mirror avatars to satisfy #14799
3. Fix a small padding error in the algorithm
This commit is contained in:
wxiaoguang 2021-11-20 01:10:41 +08:00 committed by GitHub
parent 38347aa16f
commit a8fd76557b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 417 additions and 376 deletions

View file

@ -1,23 +0,0 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
*.exe
*.test
*.prof
#vim
*.swp
#osx
.DS_Store
/testdata/*.png
.idea
.vscode

View file

@ -1,22 +0,0 @@
The MIT License (MIT)
Copyright (c) 2015 caixw
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -1,38 +0,0 @@
# identicon
[![Go](https://github.com/issue9/identicon/actions/workflows/go.yml/badge.svg)](https://github.com/issue9/identicon/actions/workflows/go.yml)
[![codecov](https://codecov.io/gh/issue9/identicon/branch/master/graph/badge.svg)](https://codecov.io/gh/issue9/identicon)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/issue9/identicon)](https://pkg.go.dev/github.com/issue9/identicon)
![Go version](https://img.shields.io/github/go-mod/go-version/issue9/identicon)
![License](https://img.shields.io/github/license/issue9/identicon)
根据用户的 IP 、邮箱名等任意数据为用户产生漂亮的随机头像。
![screenshot.1](https://raw.github.com/issue9/identicon/master/screenshot/1.png)
![screenshot.4](https://raw.github.com/issue9/identicon/master/screenshot/4.png)
![screenshot.5](https://raw.github.com/issue9/identicon/master/screenshot/5.png)
![screenshot.6](https://raw.github.com/issue9/identicon/master/screenshot/6.png)
![screenshot.7](https://raw.github.com/issue9/identicon/master/screenshot/7.png)
```go
// 根据用户访问的IP为其生成一张头像
img, _ := identicon.Make(128, color.NRGBA{},color.NRGBA{}, []byte("192.168.1.1"))
fi, _ := os.Create("/tmp/u1.png")
png.Encode(fi, img)
fi.Close()
// 或者
ii, _ := identicon.New(128, color.NRGBA{}, color.NRGBA{}, color.NRGBA{}, color.NRGBA{})
img := ii.Make([]byte("192.168.1.1"))
img = ii.Make([]byte("192.168.1.2"))
```
## 安装
```shell
go get github.com/issue9/identicon
```
## 版权
本项目采用 [MIT](https://opensource.org/licenses/MIT) 开源授权许可证,完整的授权说明可在 [LICENSE](LICENSE) 文件中找到。

View file

@ -1,716 +0,0 @@
// SPDX-License-Identifier: MIT
package identicon
import "image"
var (
// 可以出现在中间的方块,一般为了美观,都是对称图像。
centerBlocks = []blockFunc{b0, b1, b2, b3, b19, b26, b27}
// 所有方块
blocks = []blockFunc{b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27}
)
// 所有 block 函数的类型
type blockFunc func(img *image.Paletted, x, y, size int, angle int)
// 将多边形 points 旋转 angle 个角度,然后输出到 img 上,起点为 x,y 坐标
//
// points 中的坐标是基于左上角是原点的坐标系。
func drawBlock(img *image.Paletted, x, y, size int, angle int, points []int) {
if angle > 0 { // 0 角度不需要转换
m := size / 2
rotate(points, m, m, angle)
}
for i := 0; i < size; i++ {
for j := 0; j < size; j++ {
if pointInPolygon(i, j, points) {
img.SetColorIndex(x+i, y+j, 1)
}
}
}
}
// 全空白
//
// --------
// | |
// | |
// | |
// --------
func b0(img *image.Paletted, x, y, size int, angle int) {}
// 全填充正方形
//
// --------
// |######|
// |######|
// |######|
// --------
func b1(img *image.Paletted, x, y, size int, angle int) {
for i := x; i < x+size; i++ {
for j := y; j < y+size; j++ {
img.SetColorIndex(i, j, 1)
}
}
}
// 中间小方块
// ----------
// | |
// | #### |
// | #### |
// | |
// ----------
func b2(img *image.Paletted, x, y, size int, 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(i, j, 1)
}
}
}
// 菱形
//
// ---------
// | # |
// | ### |
// | ##### |
// |#######|
// | ##### |
// | ### |
// | # |
// ---------
func b3(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
drawBlock(img, x, y, size, 0, []int{
m, 0,
size, m,
m, size,
0, m,
m, 0,
})
}
// b4
//
// -------
// |#####|
// |#### |
// |### |
// |## |
// |# |
// |------
func b4(img *image.Paletted, x, y, size int, angle int) {
drawBlock(img, x, y, size, angle, []int{
0, 0,
size, 0,
0, size,
0, 0,
})
}
// b5
//
// ---------
// | # |
// | ### |
// | ##### |
// |#######|
func b5(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
drawBlock(img, x, y, size, angle, []int{
m, 0,
size, size,
0, size,
m, 0,
})
}
// b6 矩形
//
// --------
// |### |
// |### |
// |### |
// --------
func b6(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
drawBlock(img, x, y, size, angle, []int{
0, 0,
m, 0,
m, size,
0, size,
0, 0,
})
}
// b7 斜放的锥形
//
// ---------
// | # |
// | ## |
// | #####|
// | ####|
// |--------
func b7(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
drawBlock(img, x, y, size, angle, []int{
0, 0,
size, m,
size, size,
m, size,
0, 0,
})
}
// b8 三个堆叠的三角形
//
// -----------
// | # |
// | ### |
// | ##### |
// | # # |
// | ### ### |
// |#########|
// -----------
func b8(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
mm := m / 2
// 顶部三角形
drawBlock(img, x, y, size, angle, []int{
m, 0,
3 * mm, m,
mm, m,
m, 0,
})
// 底下左边
drawBlock(img, x, y, size, angle, []int{
mm, m,
m, size,
0, size,
mm, m,
})
// 底下右边
drawBlock(img, x, y, size, angle, []int{
3 * mm, m,
size, size,
m, size,
3 * mm, m,
})
}
// b9 斜靠的三角形
//
// ---------
// |# |
// | #### |
// | #####|
// | #### |
// | # |
// ---------
func b9(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
drawBlock(img, x, y, size, angle, []int{
0, 0,
size, m,
m, size,
0, 0,
})
}
// b10
//
// ----------
// | ####|
// | ### |
// | ## |
// | # |
// |#### |
// |### |
// |## |
// |# |
// ----------
func b10(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
drawBlock(img, x, y, size, angle, []int{
m, 0,
size, 0,
m, m,
m, 0,
})
drawBlock(img, x, y, size, angle, []int{
0, m,
m, m,
0, size,
0, m,
})
}
// b11 左上角1/4大小的方块
//
// ----------
// |#### |
// |#### |
// |#### |
// | |
// | |
// ----------
func b11(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
drawBlock(img, x, y, size, angle, []int{
0, 0,
m, 0,
m, m,
0, m,
0, 0,
})
}
// b12
//
// -----------
// | |
// | |
// |#########|
// | ##### |
// | # |
// -----------
func b12(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
drawBlock(img, x, y, size, angle, []int{
0, m,
size, m,
m, size,
0, m,
})
}
// b13
//
// -----------
// | |
// | |
// | # |
// | ##### |
// |#########|
// -----------
func b13(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
drawBlock(img, x, y, size, angle, []int{
m, m,
size, size,
0, size,
m, m,
})
}
// b14
//
// ---------
// | # |
// | ### |
// |#### |
// | |
// | |
// ---------
func b14(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
drawBlock(img, x, y, size, angle, []int{
m, 0,
m, m,
0, m,
m, 0,
})
}
// b15
//
// ----------
// |##### |
// |### |
// |# |
// | |
// | |
// ----------
func b15(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
drawBlock(img, x, y, size, angle, []int{
0, 0,
m, 0,
0, m,
0, 0,
})
}
// b16
//
// ---------
// | # |
// | ##### |
// |#######|
// | # |
// | ##### |
// |#######|
// ---------
func b16(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
drawBlock(img, x, y, size, angle, []int{
m, 0,
size, m,
0, m,
m, 0,
})
drawBlock(img, x, y, size, angle, []int{
m, m,
size, size,
0, size,
m, m,
})
}
// b17
//
// ----------
// |##### |
// |### |
// |# |
// | ##|
// | ##|
// ----------
func b17(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
drawBlock(img, x, y, size, angle, []int{
0, 0,
m, 0,
0, m,
0, 0,
})
quarter := size / 4
drawBlock(img, x, y, size, angle, []int{
size - quarter, size - quarter,
size, size - quarter,
size, size,
size - quarter, size,
size - quarter, size - quarter,
})
}
// b18
//
// ----------
// |##### |
// |#### |
// |### |
// |## |
// |# |
// ----------
func b18(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
drawBlock(img, x, y, size, angle, []int{
0, 0,
m, 0,
0, size,
0, 0,
})
}
// b19
//
// ----------
// |########|
// |### ###|
// |# #|
// |### ###|
// |########|
// ----------
func b19(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
drawBlock(img, x, y, size, angle, []int{
0, 0,
m, 0,
0, m,
0, 0,
})
drawBlock(img, x, y, size, angle, []int{
m, 0,
size, 0,
size, m,
m, 0,
})
drawBlock(img, x, y, size, angle, []int{
size, m,
size, size,
m, size,
size, m,
})
drawBlock(img, x, y, size, angle, []int{
0, m,
m, size,
0, size,
0, m,
})
}
// b20
//
// ----------
// | ## |
// |### |
// |## |
// |## |
// |# |
// ----------
func b20(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
q := size / 4
drawBlock(img, x, y, size, angle, []int{
q, 0,
0, size,
0, m,
q, 0,
})
}
// b21
//
// ----------
// | #### |
// |## #####|
// |## ##|
// |## |
// |# |
// ----------
func b21(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
q := size / 4
drawBlock(img, x, y, size, angle, []int{
q, 0,
0, size,
0, m,
q, 0,
})
drawBlock(img, x, y, size, angle, []int{
q, 0,
size, q,
size, m,
q, 0,
})
}
// b22
//
// ----------
// | #### |
// |## ### |
// |## ##|
// |## ##|
// |# #|
// ----------
func b22(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
q := size / 4
drawBlock(img, x, y, size, angle, []int{
q, 0,
0, size,
0, m,
q, 0,
})
drawBlock(img, x, y, size, angle, []int{
q, 0,
size, q,
size, size,
q, 0,
})
}
// b23
//
// ----------
// | #######|
// |### #|
// |## |
// |## |
// |# |
// ----------
func b23(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
q := size / 4
drawBlock(img, x, y, size, angle, []int{
q, 0,
0, size,
0, m,
q, 0,
})
drawBlock(img, x, y, size, angle, []int{
q, 0,
size, 0,
size, q,
q, 0,
})
}
// b24
//
// ----------
// | ## ###|
// |### ###|
// |## ## |
// |## ## |
// |# # |
// ----------
func b24(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
q := size / 4
drawBlock(img, x, y, size, angle, []int{
q, 0,
0, size,
0, m,
q, 0,
})
drawBlock(img, x, y, size, angle, []int{
m, 0,
size, 0,
m, size,
m, 0,
})
}
// b25
//
// ----------
// |# #|
// |## ###|
// |## ## |
// |###### |
// |#### |
// ----------
func b25(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
q := size / 4
drawBlock(img, x, y, size, angle, []int{
0, 0,
0, size,
q, size,
0, 0,
})
drawBlock(img, x, y, size, angle, []int{
0, m,
size, 0,
q, size,
0, m,
})
}
// b26
//
// ----------
// |# #|
// |### ###|
// | #### |
// |### ###|
// |# #|
// ----------
func b26(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
q := size / 4
drawBlock(img, x, y, size, angle, []int{
0, 0,
m, q,
q, m,
0, 0,
})
drawBlock(img, x, y, size, angle, []int{
size, 0,
m + q, m,
m, q,
size, 0,
})
drawBlock(img, x, y, size, angle, []int{
size, size,
m, m + q,
q + m, m,
size, size,
})
drawBlock(img, x, y, size, angle, []int{
0, size,
q, m,
m, q + m,
0, size,
})
}
// b27
//
// ----------
// |########|
// |## ###|
// |# #|
// |### ##|
// |########|
// ----------
func b27(img *image.Paletted, x, y, size int, angle int) {
m := size / 2
q := size / 4
drawBlock(img, x, y, size, angle, []int{
0, 0,
size, 0,
0, q,
0, 0,
})
drawBlock(img, x, y, size, angle, []int{
q + m, 0,
size, 0,
size, size,
q + m, 0,
})
drawBlock(img, x, y, size, angle, []int{
size, q + m,
size, size,
0, size,
size, q + m,
})
drawBlock(img, x, y, size, angle, []int{
0, size,
0, 0,
q, size,
0, size,
})
}

View file

@ -1,35 +0,0 @@
// SPDX-License-Identifier: MIT
// Package identicon 一个基于 hash 值生成随机图像的包
//
// identicon 并没有统一的标准,一般用于在用户注册时,
// 取用户的邮箱或是访问 IP 等数据(也可以是其它任何数据)
// 进行 hash 运算,之后根据 hash 数据,产生一张图像,
// 这样即可以为用户产生一张独特的头像,又不会泄漏用户的隐藏。
//
// 在 identicon 中,把图像分成以下九个部分:
// -------------
// | 1 | 2 | 3 |
// -------------
// | 4 | 5 | 6 |
// -------------
// | 7 | 8 | 9 |
// -------------
// 其中 1、3、9、7 为不同角度(依次增加 90 度)的同一张图片,
// 2、6、8、4 也是如此,这样可以保持图像是对称的,比较美观。
// 5 则单独使用一张图片。
//
// // 根据用户访问的 IP ,为其生成一张头像
// img, _ := identicon.Make(128, color.NRGBA{},color.NRGBA{}, []byte("192.168.1.1"))
// fi, _ := os.Create("/tmp/u1.png")
// png.Encode(fi, img)
// fi.Close()
//
// // 或者
// ii, _ := identicon.New(128, color.NRGBA{}, color.NRGBA{}, color.NRGBA{})
// img := ii.Make([]byte("192.168.1.1"))
// img = ii.Make([]byte("192.168.1.2"))
//
// NOTE: go test 会在当前目录的 testdata 文件夹下产生大量的随机图片。
// 要运行测试,必须保证该文件夹是存在的,且有相应的写入权限。
package identicon

View file

@ -1,5 +0,0 @@
module github.com/issue9/identicon
require github.com/issue9/assert v1.4.1
go 1.13

View file

@ -1,2 +0,0 @@
github.com/issue9/assert v1.4.1 h1:gUtOpMTeaE4JTe9kACma5foOHBvVt1p5XTFrULDwdXI=
github.com/issue9/assert v1.4.1/go.mod h1:Yktk83hAVl1SPSYtd9kjhBizuiBIqUQyj+D5SE2yjVY=

View file

@ -1,137 +0,0 @@
// SPDX-License-Identifier: MIT
package identicon
import (
"crypto/md5"
"fmt"
"image"
"image/color"
"math/rand"
)
const (
minSize = 16 // 图片的最小尺寸
maxForeColors = 32 // 在New()函数中可以指定的最大颜色数量
)
// Identicon 用于产生统一尺寸的头像
//
// 可以根据用户提供的数据,经过一定的算法,自动产生相应的图案和颜色。
type Identicon struct {
foreColors []color.Color
backColor color.Color
size int
rect image.Rectangle
}
// New 声明一个 Identicon 实例
//
// size 表示整个头像的大小;
// back 表示前景色;
// fore 表示所有可能的前景色,会为每个图像随机挑选一个作为其前景色。
func New(size int, back color.Color, fore ...color.Color) (*Identicon, error) {
if len(fore) == 0 || len(fore) > maxForeColors {
return nil, fmt.Errorf("前景色数量必须介于[1]~[%d]之间,当前为[%d]", maxForeColors, len(fore))
}
if size < minSize {
return nil, fmt.Errorf("参数 size 的值(%d)不能小于 %d", size, minSize)
}
return &Identicon{
foreColors: fore,
backColor: back,
size: size,
// 画布坐标从0开始其长度应该是 size-1
rect: image.Rect(0, 0, size, size),
}, nil
}
// Make 根据 data 数据产生一张唯一性的头像图片
func (i *Identicon) Make(data []byte) image.Image {
h := md5.New()
h.Write(data)
sum := h.Sum(nil)
b1 := int(sum[0]+sum[1]+sum[2]) % len(blocks)
b2 := int(sum[3]+sum[4]+sum[5]) % len(blocks)
c := int(sum[6]+sum[7]+sum[8]) % len(centerBlocks)
b1Angle := int(sum[9]+sum[10]) % 4
b2Angle := int(sum[11]+sum[12]) % 4
color := int(sum[11]+sum[12]+sum[15]) % len(i.foreColors)
return i.render(c, b1, b2, b1Angle, b2Angle, color)
}
// Rand 随机生成图案
func (i *Identicon) Rand(r *rand.Rand) image.Image {
b1 := r.Intn(len(blocks))
b2 := r.Intn(len(blocks))
c := r.Intn(len(centerBlocks))
b1Angle := r.Intn(4)
b2Angle := r.Intn(4)
color := r.Intn(len(i.foreColors))
return i.render(c, b1, b2, b1Angle, b2Angle, color)
}
func (i *Identicon) render(c, b1, b2, b1Angle, b2Angle, foreColor int) image.Image {
p := image.NewPaletted(i.rect, []color.Color{i.backColor, i.foreColors[foreColor]})
drawBlocks(p, i.size, centerBlocks[c], blocks[b1], blocks[b2], b1Angle, b2Angle)
return p
}
// Make 根据 data 数据产生一张唯一性的头像图片
//
// size 头像的大小。
// back, fore头像的背景和前景色。
func Make(size int, back, fore color.Color, data []byte) (image.Image, error) {
i, err := New(size, back, fore)
if err != nil {
return nil, err
}
return i.Make(data), nil
}
// 将九个方格都填上内容。
// p 为画板;
// c 为中间方格的填充函数;
// b1、b2 为边上 8 格的填充函数;
// b1Angle 和 b2Angle 为 b1、b2 的起始旋转角度。
func drawBlocks(p *image.Paletted, size int, c, b1, b2 blockFunc, b1Angle, b2Angle int) {
incr := func(a int) int {
if a >= 3 {
a = 0
} else {
a++
}
return a
}
padding := (size % 6) / 2 // 不能除尽的,边上留白。
blockSize := size / 3
twoBlockSize := 2 * blockSize
c(p, blockSize+padding, blockSize+padding, blockSize, 0)
b1(p, 0+padding, 0+padding, blockSize, b1Angle)
b2(p, blockSize+padding, 0+padding, blockSize, b2Angle)
b1Angle = incr(b1Angle)
b2Angle = incr(b2Angle)
b1(p, twoBlockSize+padding, 0+padding, blockSize, b1Angle)
b2(p, twoBlockSize+padding, blockSize+padding, blockSize, b2Angle)
b1Angle = incr(b1Angle)
b2Angle = incr(b2Angle)
b1(p, twoBlockSize+padding, twoBlockSize+padding, blockSize, b1Angle)
b2(p, blockSize+padding, twoBlockSize+padding, blockSize, b2Angle)
b1Angle = incr(b1Angle)
b2Angle = incr(b2Angle)
b1(p, 0+padding, twoBlockSize+padding, blockSize, b1Angle)
b2(p, 0+padding, blockSize+padding, blockSize, b2Angle)
}

View file

@ -1,65 +0,0 @@
// SPDX-License-Identifier: MIT
package identicon
var (
// 4 个元素分别表示 cos(0),cos(90),cos(180),cos(270)
cos = []int{1, 0, -1, 0}
// 4 个元素分别表示 sin(0),sin(90),sin(180),sin(270)
sin = []int{0, 1, 0, -1}
)
// 将 points 中的所有点,以 x,y 为原点旋转 angle 个角度。
// angle 取值只能是 [0,1,2,3],分别表示 [090180270]
func rotate(points []int, x, y int, angle int) {
if angle < 0 || angle > 3 {
panic("rotate:参数angle必须0,1,2,3三值之一")
}
for i := 0; i < len(points); i += 2 {
px, py := points[i]-x, points[i+1]-y
points[i] = px*cos[angle] - py*sin[angle] + x
points[i+1] = px*sin[angle] + py*cos[angle] + y
}
}
// 判断某个点是否在多边形之内,不包含构成多边形的线和点
// x,y 需要判断的点坐标
// points 组成多边形的所顶点,每两个元素表示一点顶点,其中最后一个顶点必须与第一个顶点相同。
func pointInPolygon(x, y int, points []int) bool {
if len(points) < 8 { // 只有2个以上的点才能组成闭合多边形
return false
}
// 大致算法如下:
// 把整个平面以给定的测试点为原点分两部分:
// - y>0包含(x>0 && y==0)
// - y<0包含(x<0 && y==0)
// 依次扫描每一个点,当该点与前一个点处于不同部分时(即一个在 y>0 区,一个在 y<0 区),
// 则判断从前一点到当前点是顺时针还是逆时针(以给定的测试点为原点),如果是顺时针 r++,否则 r--。
// 结果为2==abs(r)。
r := 0
x1, y1 := points[0], points[1]
prev := (y1 > y) || ((x1 > x) && (y1 == y))
for i := 2; i < len(points); i += 2 {
x2, y2 := points[i], points[i+1]
curr := (y2 > y) || ((x2 > x) && (y2 == y))
if curr == prev {
x1, y1 = x2, y2
continue
}
if mul := (x1-x)*(y2-y) - (x2-x)*(y1-y); mul >= 0 {
r++
} else if mul < 0 {
r--
}
x1, y1 = x2, y2
prev = curr
}
return r == 2 || r == -2
}