Skip to content

Commit

Permalink
Merge pull request #27 from ugwis/main
Browse files Browse the repository at this point in the history
Add arrowheads on line
  • Loading branch information
ugwis authored Mar 30, 2024
2 parents 7aedd26 + f1c1dc4 commit 27e67af
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 32 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
35 changes: 28 additions & 7 deletions doc/links.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,41 @@ The start and end points of the line specify the location as the 16-wind rose of

![position](static/position.png)

### Example
```
Diagrams:
Resources:
ALB: ...
PublicSubnet1Instance: ...
PublicSubnet2Instance: ...
Links:
- Source: ALB # (required)
SourcePosition: NNW # (required)
Target: PublicSubnet1Instance # (required)
TargetPosition: S # (required)
- Source: ALB # (required)
SourcePosition: NNE # (required)
Target: PublicSubnet2Instance # (required)
TargetPosition: S # (required)
```

### Arrow head

Arrows add context and meaning to a diagram by indicating the direction of flow.

![arrow head](static/arrows.png)
(generated from [static/arrows.yaml](static/arrows.yaml))

```
- Source: ALB
SourcePosition: NNW
Target: PublicSubnet1Instance
TargetPosition: S
- Source: ALB
SourcePosition: NNE
Target: PublicSubnet2Instance
TargetPosition: S
SourceArrowHead: #(optional)
Type: Open #(required) Open/Default
Width: Default # (optional) Narrow/Default/Wide default="Default"
Length: 2 # (optional) default=2
Target: VPCPublicSubnet1Instance
TargetPosition: SSE
TargetArrowHead: #(optional)
Type: Open #(required) Open/Default
Width: Default # (optional) Narrow/Default/Wide default="Default"
Length: 2 # (optional) default=2
```
Binary file added doc/static/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 doc/static/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
Binary file modified examples/alb-ec2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions examples/alb-ec2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,23 @@ Diagram:
SourcePosition: NNW
Target: VPCPublicSubnet1Instance
TargetPosition: SSE
TargetArrowHead:
Type: Open
- Source: ALB
SourcePosition: NNE
Target: VPCPublicSubnet2Instance
TargetPosition: SSW
TargetArrowHead:
Type: Open
- Source: IGW
SourcePosition: N
Target: ALB
TargetPosition: S
TargetArrowHead:
Type: Open
- Source: User
SourcePosition: N
Target: IGW
TargetPosition: S
TargetArrowHead:
Type: Open
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 27e67af

Please sign in to comment.