// NewGLTriangles returns GLTriangles initialized with the data from the supplied Triangles.
//
// Only draw the Triangles using the provided Shader.
353198
func NewGLTriangles(shader *glhf.Shader, t pixel.Triangles) *GLTriangles {
	var gt *GLTriangles
	mainthread.Call(func() {
		gt = &GLTriangles{
			vs:     glhf.MakeVertexSlice(shader, 0, t.Len()),
			shader: shader,
		}
	})
	gt.SetLen(t.Len())
	gt.Update(t)
	return gt
}
163348
func NewLineString(coordinates Coordinates) *LineString {
	if coordinates == nil {
		coordinates = make(Coordinates, 0, INIT_GEOM_CAP)
	}
	return &LineString{Type: "LineString", Coordinates: coordinates}
}
464504
func (gt *GLTriangles) SetLen(length int) {
	switch {
	case length > gt.Len():
		needAppend := length - gt.Len()
		for i := 0; i < needAppend; i++ {
			gt.data = append(gt.data,
				0, 0,
				1, 1, 1, 1,
				0, 0,
				0,
			)
		}
	case length < gt.Len():
		gt.data = gt.data[:length*gt.vs.Stride()]
	default:
		return
	}
	mainthread.Call(func() {
		gt.vs.Begin()
		gt.vs.SetLen(length)
		gt.vs.End()
	})
}
493999
func NewMultiPolygon(coordinates []MultiLine) *MultiPolygon {
	if coordinates == nil {
		coordinates = make([]MultiLine, 0, INIT_GEOM_CAP)
	}
	return &MultiPolygon{Type: "MultiPolygon", Coordinates: coordinates}
}
49203
func newIntegrals(src image.Image) (*integral, *integral) {
	b := src.Bounds()
	srcg, ok := src.(*image.Gray)
	if !ok {
		srcg = image.NewGray(b)
		draw.Draw(srcg, b, src, b.Min, draw.Src)
	}

	m := integral{
		pix:    make([]uint64, b.Max.Y*b.Max.X),
		stride: b.Max.X,
		rect:   b,
	}
	mSq := integral{
		pix:    make([]uint64, b.Max.Y*b.Max.X),
		stride: b.Max.X,
		rect:   b,
	}
	for y := b.Min.Y; y < b.Max.Y; y++ {
		for x := b.Min.X; x < b.Max.X; x++ {
			os := (y-b.Min.Y)*srcg.Stride + x - b.Min.X
			om := (y-b.Min.Y)*m.stride + x - b.Min.X
			c := uint64(srcg.Pix[os])
			m.pix[om] = c
			mSq.pix[om] = c * c
		}
	}
	m.integrate()
	mSq.integrate()
	return &m, &mSq
}
806688
func (g *Gui) drawFrameCorners(v *View, fgColor, bgColor Attribute) error {
	runeTL, runeTR, runeBL, runeBR := '┌', '┐', '└', '┘'
	if g.ASCII {
		runeTL, runeTR, runeBL, runeBR = '+', '+', '+', '+'
	}

	corners := []struct {
		x, y int
		ch   rune
	}{{v.x0, v.y0, runeTL}, {v.x1, v.y0, runeTR}, {v.x0, v.y1, runeBL}, {v.x1, v.y1, runeBR}}

	for _, c := range corners {
		if c.x >= 0 && c.y >= 0 && c.x < g.maxX && c.y < g.maxY {
			if err := g.SetRune(c.x, c.y, c.ch, fgColor, bgColor); err != nil {
				return err
			}
		}
	}
	return nil
}
748367
func (document *Document) GetPartPageRectangles() []image.Rectangle {
	var rectangles []image.Rectangle
	rawRectangles := C.GoString(C.get_part_page_rectangles(document.handle))
	pageRectangles := strings.Split(rawRectangles, ";")
	for _, points := range pageRectangles {
		var intPoints []int
		strPoints := strings.Split(points, ",")
		for _, point := range strPoints {
			point = strings.Trim(point, " ")
			i, _ := strconv.Atoi(point)
			intPoints = append(intPoints, i)
		}
		x0, y0 := intPoints[0], intPoints[1]
		x1, y1 := x0+intPoints[2], y0+intPoints[3]
		rectangles = append(rectangles, image.Rect(x0, y0, x1, y1))
	}
	return rectangles
}
879406
func (glpeer *GLPeer) NewNode(fn arrangerFunc) *ZNode {
	glpeer.mu.Lock()
	defer glpeer.mu.Unlock()
	n := &sprite.Node{Arranger: fn}
	glpeer.eng.Register(n)
	return &ZNode{Node: n}
}
409308
func (p *Pattern) geoTriangles() {
	scale := utils.HexVal(p.Hash, 0, 1)
	sideLength := utils.Map(scale, 0, 15, 15, 80)
	triangleHeight := sideLength / 2 * math.Sqrt(3)
	triangle := shapes.BuildTriangle(sideLength, triangleHeight)

	p.Svg.SetHeight(int(triangleHeight * 6))
	p.Svg.SetWidth(int(sideLength * 3))

	i := 0
	for y := 0; y <= 5; y++ {
		for x := 0; x <= 5; x++ {

			val := utils.HexVal(p.Hash, i, 1)
			opacity := utils.Opacity(val)
			fill := utils.FillColor(val)

			styles := make(map[string]interface{})
			styles["fill"] = fill
			styles["fill-opacity"] = opacity
			styles["stroke"] = utils.StrokeColor
			styles["stroke-opacity"] = utils.StrokeOpacity

			var rotation int
			if y%2 == 0 {
				if x%2 == 0 {
					rotation = 180
				}
			} else {
				if x%2 != 0 {
					rotation = 180
				}
			}

			style := make(map[string]interface{})

			style["transform"] = fmt.Sprintf("translate(%v, %v) rotate(%v, %v, %v)", float64(x)*sideLength*0.5-sideLength/2, triangleHeight*float64(y), rotation, sideLength/2, triangleHeight/2)
			p.Svg.Polyline(triangle, utils.Merge(styles, style))

			if x == 0 {
				style["transform"] = fmt.Sprintf("translate(%v, %v) rotate(%v, %v, %v)", 6*sideLength*0.5-sideLength/2, triangleHeight*float64(y), rotation, sideLength/2, triangleHeight/2)
				p.Svg.Polyline(triangle, utils.Merge(styles, style))
			}

			i = i + 1
		}
	}
}
432125
func DrawElementsInstanced(mode uint32, count int32, xtype uint32, indices unsafe.Pointer, instancecount int32) {
	C.glowDrawElementsInstanced(gpDrawElementsInstanced, (C.GLenum)(mode), (C.GLsizei)(count), (C.GLenum)(xtype), indices, (C.GLsizei)(instancecount))
}
745337
func roundAngle(val s1.Angle) int32 {
	if val < 0 {
		return int32(val - 0.5)
	}
	return int32(val + 0.5)
}
49059
func (p *Polygon) initEdgesAndIndex() {
	if p.IsFull() {
		return
	}
	const maxLinearSearchLoops = 12 // Based on benchmarks.
	if len(p.loops) > maxLinearSearchLoops {
		p.cumulativeEdges = make([]int, 0, len(p.loops))
	}

	for _, l := range p.loops {
		if p.cumulativeEdges != nil {
			p.cumulativeEdges = append(p.cumulativeEdges, p.numEdges)
		}
		p.numEdges += len(l.vertices)
	}

	p.index = NewShapeIndex()
	p.index.Add(p)
}
781729
func (bb BoundingBox) Geometry() interface{} {
	var result interface{}
	switch len(bb) {
	case 4:
		if (bb[0] == bb[2]) && (bb[1] == bb[3]) {
			result = NewPoint(bb[0:2])
		} else {
			coordinates := make([][][]float64, 1)
			coordinates[0] = make([][]float64, 5)
			coordinates[0][0] = make([]float64, 2)
			coordinates[0][0][0] = bb[0]
			coordinates[0][0][1] = bb[1]
			coordinates[0][1] = make([]float64, 2)
			coordinates[0][1][0] = bb[2]
			coordinates[0][1][1] = bb[1]
			coordinates[0][2] = make([]float64, 2)
			coordinates[0][2][0] = bb[2]
			coordinates[0][2][1] = bb[3]
			coordinates[0][3] = make([]float64, 2)
			coordinates[0][3][0] = bb[0]
			coordinates[0][3][1] = bb[3]
			coordinates[0][4] = make([]float64, 2)
			coordinates[0][4][0] = bb[0]
			coordinates[0][4][1] = bb[1]
			result = NewPolygon(coordinates)
		}
	}
	return result
}
107436
func GenericRings(bg BasicGraphics, wedges []Wedgeinfo, x, y, ro, ri int, eccentricity float64) {
	// DebugLogger.Printf("GenericRings with %d wedges center %d,%d, radii %d/%d,  ecc=%.3f)", len(wedges), x, y, ro, ri, eccentricity)

	for _, w := range wedges {

		// Correct center
		d := float64(w.Style.LineWidth) / 2

		// cphi, sphi := math.Cos(w.Phi), math.Sin(w.Phi)
		// cpsi, spsi := math.Cos(w.Psi), math.Sin(w.Psi)
		delta := (w.Psi - w.Phi) / 2
		SinDelta := math.Sin(delta)
		gamma := (w.Phi + w.Psi) / 2
		k := d / SinDelta
		shift := float64(w.Shift)
		kx, ky := (k+shift)*math.Cos(gamma), (k+shift)*math.Sin(gamma)

		DebugLogger.Printf("Center adjustment (lw=%d, d=%.2f), for wedge %d°-%d° of (%.1f,%.1f), k=%.1f",
			w.Style.LineWidth, d, int(180*w.Phi/math.Pi), int(180*w.Psi/math.Pi), kx, ky, k)

		xi, yi := x+int(kx+0.5), y+int(ky+0.5)
		roc, ric := ro-int(d+k), ri-int(d+k)
		bg.Wedge(xi, yi, roc, ric, w.Phi, w.Psi, w.Style)

		if w.Text != "" {
			_, fh, _ := bg.FontMetrics(w.Font)
			fh += 0
			alpha := (w.Phi + w.Psi) / 2
			var rt int
			if ri > 0 {
				rt = (ri + ro) / 2
			} else {
				rt = ro - 3*fh
				if rt <= ro/2 {
					rt = ro - 2*fh
				}
			}
			// DebugLogger.Printf("Text %s at %d° r=%d", w.Text, int(180*alpha/math.Pi), rt)
			tx := int(float64(rt)*math.Cos(alpha)*eccentricity+0.5) + x
			ty := y + int(float64(rt)*math.Sin(alpha)+0.5)

			bg.Text(tx, ty, w.Text, "cc", 0, w.Font)
		}

	}

}
640173
func BuildRotatedTriangle(sideLength, width float64) string {
	halfHeight := sideLength / 2

	return fmt.Sprintf("0,0,%v,%v,0,%v,0,0", width, halfHeight, sideLength)
}
646111
func (s *DistributionSummary) SetOrigins(v *Origins) *DistributionSummary {
	s.Origins = v
	return s
}
762822
func DrawArraysInstanced(mode uint32, first int32, count int32, instancecount int32) {
	C.glowDrawArraysInstanced(gpDrawArraysInstanced, (C.GLenum)(mode), (C.GLint)(first), (C.GLsizei)(count), (C.GLsizei)(instancecount))
}