drawing

package
v0.1.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 27, 2025 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UnitLength         = 100
	DefaultLength      = 1 * UnitLength
	DefaultStrokeWidth = 5
)

Variables

View Source
var CommandTypeToFunc = map[string]func(*Schematic){
	"push": func(s *Schematic) {
		s.Push(s.Pos)
	},
	"pop": func(s *Schematic) {
		s.Pos = s.Pop()
	},
}
View Source
var ElemTypeToRenderFunc = map[string]func(*Schematic, Point, Point){
	"line": func(s *Schematic, p1, p2 Point) {
		s.addPath(createPath(
			'M', Point{p1.X, p1.Y},
			'L', Point{p2.X, p2.Y},
		))
	},
	"resistor": func(s *Schematic, p1, p2 Point) {
		const (
			height = DefaultLength / 4
			width  = DefaultLength / 2
		)

		distance := p1.distanceTo(p2)

		s.addAndPivotPath(p1, p2, createPath(
			'M', Point{p1.X, p1.Y},
			'L', Point{p1.X + distance/2 - width/2, p1.Y},
			'L', Point{p1.X + distance/2 - width/2, p1.Y + height/2},
			'L', Point{p1.X + distance/2 + width/2, p1.Y + height/2},
			'L', Point{p1.X + distance/2 + width/2, p1.Y - height/2},
			'L', Point{p1.X + distance/2 - width/2, p1.Y - height/2},
			'L', Point{p1.X + distance/2 - width/2, p1.Y},
			'M', Point{p1.X + distance/2 + width/2, p1.Y},
			'L', Point{p1.X + distance, p1.Y},
		))
	},
	"battery": func(s *Schematic, p1, p2 Point) {
		const (
			termGap       = DefaultLength / 6
			negTermHeight = DefaultLength / 6
			posTermHeight = DefaultLength / 3
		)

		distance := p1.distanceTo(p2)
		negTermX := p1.X + distance/2 - termGap/2
		posTermX := negTermX + termGap

		s.addAndPivotPath(p1, p2, createPath(
			'M', Point{p1.X, p1.Y},
			'L', Point{negTermX, p1.Y},
			'M', Point{negTermX, p1.Y - negTermHeight/2},
			'L', Point{negTermX, p1.Y + negTermHeight/2},
			'M', Point{posTermX, p1.Y - posTermHeight/2},
			'L', Point{posTermX, p1.Y + posTermHeight/2},
			'M', Point{posTermX, p1.Y},
			'L', Point{p1.X + distance, p1.Y},
		))
	},
	"dot": func(s *Schematic, p1, p2 Point) {
		const radius = DefaultLength / 25
		s.addCircle(p1.X, p1.Y, radius)
	},
	"capacitor": func(s *Schematic, p1, p2 Point) {
		const (
			gap    = DefaultLength / 6
			height = DefaultLength / 3
		)

		distance := p1.distanceTo(p2)
		negTermX := p1.X + distance/2 - gap/2
		posTermX := negTermX + gap

		s.addAndPivotPath(p1, p2, createPath(
			'M', Point{p1.X, p1.Y},
			'L', Point{negTermX, p1.Y},
			'M', Point{negTermX, p1.Y - height/2},
			'L', Point{negTermX, p1.Y + height/2},
			'M', Point{posTermX, p1.Y - height/2},
			'L', Point{posTermX, p1.Y + height/2},
			'M', Point{posTermX, p1.Y},
			'L', Point{p1.X + distance, p1.Y},
		))
	},
	"inductor": func(s *Schematic, p1, p2 Point) {
		const (
			width        = DefaultLength
			coilLoopSize = width / 8
		)

		distance := p1.distanceTo(p2)

		s.addAndPivotPath(p1, p2, createPath(
			'M', Point{p1.X, p1.Y},
			'l', Point{distance/2 - width/2, 0},

			'q',
			Point{0, -coilLoopSize},
			Point{coilLoopSize, -coilLoopSize},
			'q',
			Point{coilLoopSize, 0},
			Point{coilLoopSize, coilLoopSize},
			'q',
			Point{0, -coilLoopSize},
			Point{coilLoopSize, -coilLoopSize},
			'q',
			Point{coilLoopSize, 0},
			Point{coilLoopSize, coilLoopSize},
			'q',
			Point{0, -coilLoopSize},
			Point{coilLoopSize, -coilLoopSize},
			'q',
			Point{coilLoopSize, 0},
			Point{coilLoopSize, coilLoopSize},
			'q',
			Point{0, -coilLoopSize},
			Point{coilLoopSize, -coilLoopSize},
			'q',
			Point{coilLoopSize, 0},
			Point{coilLoopSize, coilLoopSize},

			'L', Point{p1.X + distance, p1.Y},
		))
	},
	"sourceV": func(s *Schematic, p1, p2 Point) {
		const (
			radius = DefaultLength / 8
		)

		circleStyle := fmt.Sprintf(`fill="none" stroke="black" stroke-width="%d"`, DefaultStrokeWidth)
		s.addCircle(p1.X, p1.Y, radius, circleStyle)

	},
}

Functions

This section is empty.

Types

type Circle

type Circle struct {
	// contains filtered or unexported fields
}

type Path

type Path []command

type Point

type Point struct {
	X, Y float64
}

type Schematic

type Schematic struct {
	Pos     Point
	Paths   []*Path
	Circles []*Circle
	// contains filtered or unexported fields
}

func NewSchematic

func NewSchematic() *Schematic

func (*Schematic) End

func (s *Schematic) End(buf *bytes.Buffer)

func (*Schematic) Normalise

func (s *Schematic) Normalise() (width, height float64)

FIXME: this is one ugly function..

func (*Schematic) Pop added in v0.1.7

func (s *Schematic) Pop() Point

func (*Schematic) Push added in v0.1.7

func (s *Schematic) Push(p Point)

func (*Schematic) Translate

func (s *Schematic) Translate(dx, dy float64) *Schematic

type Stack

type Stack struct {
	// contains filtered or unexported fields
}

func (*Stack) Pop

func (s *Stack) Pop() Point

func (*Stack) Push

func (s *Stack) Push(item Point)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL