MCPcopy
hub / github.com/tdewolff/canvas / FastBounds

Method FastBounds

path.go:1056–1105  ·  view source on GitHub ↗

FastBounds returns the maximum bounding box rectangle of the path. It is quicker than Bounds.

()

Source from the content-addressed store, hash-verified

1054
1055// FastBounds returns the maximum bounding box rectangle of the path. It is quicker than Bounds.
1056func (p *Path) FastBounds() Rect {
1057 if len(p.d) < 4 {
1058 return Rect{}
1059 }
1060
1061 // first command is MoveTo
1062 start, end := Point{p.d[1], p.d[2]}, Point{}
1063 xmin, xmax := start.X, start.X
1064 ymin, ymax := start.Y, start.Y
1065 for i := 4; i < len(p.d); {
1066 cmd := p.d[i]
1067 switch cmd {
1068 case MoveToCmd, LineToCmd, CloseCmd:
1069 end = Point{p.d[i+1], p.d[i+2]}
1070 xmin = math.Min(xmin, end.X)
1071 xmax = math.Max(xmax, end.X)
1072 ymin = math.Min(ymin, end.Y)
1073 ymax = math.Max(ymax, end.Y)
1074 case QuadToCmd:
1075 cp := Point{p.d[i+1], p.d[i+2]}
1076 end = Point{p.d[i+3], p.d[i+4]}
1077 xmin = math.Min(xmin, math.Min(cp.X, end.X))
1078 xmax = math.Max(xmax, math.Max(cp.X, end.X))
1079 ymin = math.Min(ymin, math.Min(cp.Y, end.Y))
1080 ymax = math.Max(ymax, math.Max(cp.Y, end.Y))
1081 case CubeToCmd:
1082 cp1 := Point{p.d[i+1], p.d[i+2]}
1083 cp2 := Point{p.d[i+3], p.d[i+4]}
1084 end = Point{p.d[i+5], p.d[i+6]}
1085 xmin = math.Min(xmin, math.Min(cp1.X, math.Min(cp2.X, end.X)))
1086 xmax = math.Max(xmax, math.Max(cp1.X, math.Min(cp2.X, end.X)))
1087 ymin = math.Min(ymin, math.Min(cp1.Y, math.Min(cp2.Y, end.Y)))
1088 ymax = math.Max(ymax, math.Max(cp1.Y, math.Min(cp2.Y, end.Y)))
1089 case ArcToCmd:
1090 rx, ry, phi := p.d[i+1], p.d[i+2], p.d[i+3]
1091 large, sweep := toArcFlags(p.d[i+4])
1092 end = Point{p.d[i+5], p.d[i+6]}
1093 cx, cy, _, _ := ellipseToCenter(start.X, start.Y, rx, ry, phi, large, sweep, end.X, end.Y)
1094 r := math.Max(rx, ry)
1095 xmin = math.Min(xmin, cx-r)
1096 xmax = math.Max(xmax, cx+r)
1097 ymin = math.Min(ymin, cy-r)
1098 ymax = math.Max(ymax, cy+r)
1099
1100 }
1101 i += cmdLen(cmd)
1102 start = end
1103 }
1104 return Rect{xmin, ymin, xmax, ymax}
1105}
1106
1107// Bounds returns the exact bounding box rectangle of the path.
1108func (p *Path) Bounds() Rect {

Callers 6

TileMethod · 0.95
RenderPathMethod · 0.95
parseDefsMethod · 0.80
BenchmarkToRasterizerFunction · 0.80
BenchmarkToScanxFunction · 0.80
TileMethod · 0.80

Calls 3

toArcFlagsFunction · 0.85
ellipseToCenterFunction · 0.85
cmdLenFunction · 0.85

Tested by 2

BenchmarkToRasterizerFunction · 0.64
BenchmarkToScanxFunction · 0.64