shape_detect.go

Functions Structs

Functions

func DetectShape

DetectShape analyzes a Path and returns the identified shape if recognized.

Returns a DetectedShape with Kind == ShapeUnknown if the path cannot be

identified as a simple geometric primitive.

func DetectShape(path *Path) DetectedShape {
	if path == nil {
		return DetectedShape{Kind: ShapeUnknown}
	}

	nv := path.NumVerbs()
	if nv == 0 {
		return DetectedShape{Kind: ShapeUnknown}
	}

	verbs := path.Verbs()
	coords := path.Coords()

	// Try circle/ellipse: MoveTo + 4xCubicTo + Close = 6 elements
	if nv == 6 {
		if shape, ok := detectCircleOrEllipse(verbs, coords); ok {
			return shape
		}
	}

	// Try rect: MoveTo + 3xLineTo + Close = 5 elements
	if nv == 5 {
		if shape, ok := detectRect(verbs, coords); ok {
			return shape
		}
	}

	// Try rrect: MoveTo + (CubicTo + LineTo)*4 + Close = 10 elements
	// Or variation with arcs: more elements
	if nv >= 9 {
		if shape, ok := detectRRect(verbs, coords); ok {
			return shape
		}
	}

	return DetectedShape{Kind: ShapeUnknown}
}

Structs

type DetectedShape struct

DetectedShape holds parameters of a recognized geometric shape.

The Kind field indicates which parameters are meaningful.

type DetectedShape struct {
	Kind		ShapeKind
	CenterX		float64	// Center X coordinate.
	CenterY		float64	// Center Y coordinate.
	RadiusX		float64	// X radius. For circle: RadiusX == RadiusY.
	RadiusY		float64	// Y radius. For circle: RadiusX == RadiusY.
	Width		float64	// Total width for rect/rrect.
	Height		float64	// Total height for rect/rrect.
	CornerRadius	float64	// Corner radius for rrect only.
}