Skip to content

Commit

Permalink
add: arrow head on line
Browse files Browse the repository at this point in the history
  • Loading branch information
ugwis committed Mar 30, 2024
1 parent 7aedd26 commit b74f6e8
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 25 deletions.
14 changes: 8 additions & 6 deletions cmd/awsdac/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ type Resource struct {
}

type Link struct {
Source string `yaml:"Source"`
SourcePosition string `yaml:"SourcePosition"`
Target string `yaml:"Target"`
TargetPosition string `yaml:"TargetPosition"`
LineWidth int `yaml:"LineWidth"`
Source string `yaml:"Source"`
SourcePosition string `yaml:"SourcePosition"`
SourceArrowHead types.ArrowHead `yaml:"SourceArrowHead"`
Target string `yaml:"Target"`
TargetPosition string `yaml:"TargetPosition"`
TargetArrowHead types.ArrowHead `yaml:"TargetArrowHead"`
LineWidth int `yaml:"LineWidth"`
}

func main() {
Expand Down Expand Up @@ -234,7 +236,7 @@ func main() {
if lineWidth == 0 {
lineWidth = 2
}
link := new(types.Link).Init(&source, v.SourcePosition, &target, v.TargetPosition, lineWidth)
link := new(types.Link).Init(&source, v.SourcePosition, v.SourceArrowHead, &target, v.TargetPosition, v.TargetArrowHead, lineWidth)
resources[v.Source].AddLink(link)
resources[v.Target].AddLink(link)
}
Expand Down
Binary file added examples/arrows.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
119 changes: 119 additions & 0 deletions examples/arrows.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
Diagram:
DefinitionFiles:
- Type: URL
Url: "https://raw.githubusercontent.com/awslabs/diagram-as-code/main/definitions/definition-for-aws-icons-light.yaml"
#- Type: LocalFile
# LocalFile: ./definitions/definition-for-aws-icons-light.yaml

Resources:
Canvas:
Type: AWS::Diagram::Canvas
Direction: vertical
Children:
- ArrowTitleStack
- ArrowNarrowStack
- ArrowDefaultStack
- ArrowWideStack

ArrowTitleStack:
Type: AWS::Diagram::HorizontalStack
Children:
- ArrowTitle
- ArrowTitleOpen
- ArrowTitleDefault
ArrowTitle:
Type: AWS::Diagram::Resource
Title: Width\Type
ArrowTitleOpen:
Type: AWS::Diagram::Resource
Title: Open
ArrowTitleDefault:
Type: AWS::Diagram::Resource
Title: Default

ArrowNarrowStack:
Type: AWS::Diagram::HorizontalStack
Children:
- ArrowNarrowTitle
- ArrowNarrowOpen
- ArrowNarrowDefault
ArrowNarrowTitle:
Type: AWS::Diagram::Resource
Title: Narrow
ArrowNarrowOpen:
Type: AWS::Diagram::Resource
ArrowNarrowDefault:
Type: AWS::Diagram::Resource

ArrowDefaultStack:
Type: AWS::Diagram::HorizontalStack
Children:
- ArrowDefaultTitle
- ArrowDefaultOpen
- ArrowDefaultDefault
ArrowDefaultTitle:
Type: AWS::Diagram::Resource
Title: Default
ArrowDefaultOpen:
Type: AWS::Diagram::Resource
ArrowDefaultDefault:
Type: AWS::Diagram::Resource

ArrowWideStack:
Type: AWS::Diagram::HorizontalStack
Children:
- ArrowWideTitle
- ArrowWideOpen
- ArrowWideDefault
ArrowWideTitle:
Type: AWS::Diagram::Resource
Title: Wide
ArrowWideOpen:
Type: AWS::Diagram::Resource
ArrowWideDefault:
Type: AWS::Diagram::Resource


Links:
- Source: ArrowNarrowTitle
SourcePosition: E
Target: ArrowNarrowOpen
TargetPosition: W
TargetArrowHead:
Type: Open
Width: Narrow
- Source: ArrowNarrowOpen
SourcePosition: E
Target: ArrowNarrowDefault
TargetPosition: W
TargetArrowHead:
Type: Default
Width: Narrow
- Source: ArrowDefaultTitle
SourcePosition: E
Target: ArrowDefaultOpen
TargetPosition: W
TargetArrowHead:
Type: Open
Width: Default
- Source: ArrowDefaultOpen
SourcePosition: E
Target: ArrowDefaultDefault
TargetPosition: W
TargetArrowHead:
Type: Default
Width: Default
- Source: ArrowWideTitle
SourcePosition: E
Target: ArrowWideOpen
TargetPosition: W
TargetArrowHead:
Type: Open
Width: Wide
- Source: ArrowWideOpen
SourcePosition: E
Target: ArrowWideDefault
TargetPosition: W
TargetArrowHead:
Type: Default
Width: Wide
103 changes: 84 additions & 19 deletions internal/types/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,31 @@ import (
)

type Link struct {
Source *Node
SourcePosition string
Target *Node
TargetPosition string
LineWidth int
drawn bool
lineColor color.RGBA
Source *Node
SourcePosition string
SourceArrowHead ArrowHead
Target *Node
TargetPosition string
TargetArrowHead ArrowHead
LineWidth int
drawn bool
lineColor color.RGBA
}

func (l Link) Init(source *Node, sourcePosition string, target *Node, targetPosition string, lineWidth int) *Link {
type ArrowHead struct {
Type string `yaml:"Type"`
Length float64 `yaml:"Length"`
Width string `yaml:"Width"`
}

func (l Link) Init(source *Node, sourcePosition string, sourceArrowHead ArrowHead, target *Node, targetPosition string, targetArrowHead ArrowHead, lineWidth int) *Link {
gl := Link{}
gl.Source = source
gl.SourcePosition = sourcePosition
gl.SourceArrowHead = sourceArrowHead
gl.Target = target
gl.TargetPosition = targetPosition
gl.TargetArrowHead = targetArrowHead
gl.LineWidth = lineWidth
gl.drawn = false
gl.lineColor = color.RGBA{0, 0, 0, 255}
Expand All @@ -50,17 +60,7 @@ func (l *Link) drawNeighborsDot(img *image.RGBA, x, y float64) {
}
}

func (l *Link) Draw(img *image.RGBA) {
source := *l.Source
target := *l.Target
if l.drawn {
log.Info("Link already drawn")
return
}
log.Info("Link Drawing")
sourcePt, _ := calcPosition(source.GetBindings(), l.SourcePosition)
targetPt, _ := calcPosition(target.GetBindings(), l.TargetPosition)

func (l *Link) drawLine(img *image.RGBA, sourcePt image.Point, targetPt image.Point) {
dx := float64(targetPt.X - sourcePt.X)
dy := float64(targetPt.Y - sourcePt.Y)
length := math.Sqrt(math.Pow(dx, 2) + math.Pow(dy, 2))
Expand All @@ -76,5 +76,70 @@ func (l *Link) Draw(img *image.RGBA) {
l.drawNeighborsDot(img, x+wx, y+wy)
}
}
}

func (l *Link) getThreeSide(t string) (float64, float64, float64) {
switch t {
case "Narrow":
return math.Sqrt(3.0), 2.0, 1.0
case "Default", "":
return 1.0, math.Sqrt(2.0), 1.0
case "Wide":
return 1.0, 2.0, math.Sqrt(3.0)
}
return 0, 0, 0
}

func (l *Link) drawArrowHead(img *image.RGBA, arrowPt image.Point, originPt image.Point, arrowHead ArrowHead) {
dx := float64(arrowPt.X - originPt.X)
dy := float64(arrowPt.Y - originPt.Y)
length := math.Sqrt(math.Pow(dx, 2) + math.Pow(dy, 2))
if arrowHead.Length == 0 {
arrowHead.Length = 10
}
log.Info(arrowHead.Width)
_a, _b, _c := l.getThreeSide(arrowHead.Width)
at1 := arrowPt.Sub(image.Point{
int(arrowHead.Length * (_a*dx - _c*dy) / (_b * length)),
int(arrowHead.Length * (_c*dx + _a*dy) / (_b * length)),
})
at2 := arrowPt.Sub(image.Point{
int(arrowHead.Length * (_a*dx + _c*dy) / (_b * length)),
int(arrowHead.Length * (-_c*dx + _a*dy) / (_b * length)),
})

switch arrowHead.Type {
case "Default":
log.Info("Default Arrow Head drawing")
al := int(arrowHead.Length)
for i := 0; i < al; i++ {
a := arrowPt.Mul(i)
b := a.Add(at1.Mul(al - i)).Div(al)
c := a.Add(at2.Mul(al - i)).Div(al)
l.drawLine(img, b, c)
}
case "Open":
log.Info("Open Arrow Head drawing")
l.drawLine(img, arrowPt, at1)
l.drawLine(img, arrowPt, at2)
}
}

func (l *Link) Draw(img *image.RGBA) {
source := *l.Source
target := *l.Target
if l.drawn {
log.Info("Link already drawn")
return
}
log.Info("Link Drawing")
sourcePt, _ := calcPosition(source.GetBindings(), l.SourcePosition)
targetPt, _ := calcPosition(target.GetBindings(), l.TargetPosition)

l.drawLine(img, sourcePt, targetPt)

l.drawArrowHead(img, sourcePt, targetPt, l.SourceArrowHead)
l.drawArrowHead(img, targetPt, sourcePt, l.TargetArrowHead)

l.drawn = true
}

0 comments on commit b74f6e8

Please sign in to comment.