Skip to content

Implement graph, nodes and edge creation #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 13 commits into
base: api-demos
Choose a base branch
from
4 changes: 4 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
VideoIO = "d6d074c3-1acf-5d4c-9a43-ef38773959a2"
LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d"
GraphPlot = "a2cc645c-3eea-5389-862e-a155d0052231"

[compat]
Animations = "0.4"
Expand All @@ -39,3 +41,5 @@ Luxor = "2"
ProgressMeter = "1"
VideoIO = "0.6, 0.7, 0.8"
julia = "1.4"
LightGraphs = "1.3.5"
GraphPlot = "0.4.4"
Binary file added examples/graph_animations/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions examples/graph_animations/demo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
```julia
using Javis, LaTeXStrings

function edge(; p1, p2, kwargs...)
# sethue("black")
# ob = blend(p1, p2, "orange", "blue")
# setblend(ob)
line(p1, p2, :clip)
rect(p1, (p2-p1)..., :fill)
# rect(p1, p2, :fill)
clipreset()
end

function node(p=O, color="black")
sethue(color)
circle(p, 10, :fill)
return p
end

function ground(args...)
background("white")
sethue("black")
end

video = Video(400, 400)
Background(1:150, ground)

g = @Object 1:150 JGraph(true, 100, 100) O
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the O doesn't have any effect?
For example replacing it by Point(-100,0) does not shift it 100px to the left


adjacency_list = [[2, 3, 4, 5, 6],
[7, 8],
[7, 8],
[],[],[],[],[]]
for i in 1:length(adjacency_list)
if i%2 == 0
@Graph g i*10:150 GraphVertex(i, [node_shape(:circle, true, radius=12), node_fill(:image, "football.png")]) O
else
@Graph g i*10:150 GraphVertex(i, [node_shape(:rectangle, true, width=20, height=20), node_fill(:color, "yellow"), node_text(L"""%$i""", :inside), node_border("green", 2)]) O
end
end
# , node_text("$(i)", :top)
global count = 0
for i in 1:length(adjacency_list)
for j in adjacency_list[i]
@Graph g 15+count*10:150 GraphEdge(i, j, [edge_shape(:curved, false; center_offset=5, end_offsets=(15, 15))]) O
count+=1
end
end

# c = 5
# i = 1
# j = 2
# g2 = @Object 1:150 JGraph(true, 100, 100) O
# @Graph g2 c*10:150 GraphVertex(i, (args...; kw...)->node()) O
# @Graph g2 c*10:150 GraphVertex(j, (args...; kw...)->node()) O
# @Graph g2 c*10:150 GraphEdge(i, j, (args...; kw...)->edge(; kw...)) O

render(video; pathname="demo.gif")
```

![Demo](demo.gif)
Binary file added examples/graph_animations/demo2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions examples/graph_animations/demo2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
```julia
using Javis, LaTeXStrings

function edge(; p1, p2, kwargs...)
sethue("black")
line(p1, p2, :stroke)
end

function node(position)
# Just return the node position
return Dict(:position=>position), (args...; kwargs...) -> nothing
end
Comment on lines +11 to +12
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this notation seems a bit weird. What are the two return arguments?


function ground(args...)
background("white")
sethue("black")
end

video = Video(400, 400)
Background(1:150, ground)

g = @Object 1:150 JGraph(true, 100, 100, :none) Point(60, 60)

adjacency_list = [[2, 3, 4, 5, 6],
[7, 8],
[7, 8],
[],[],[],[],[]]
coords = []
for i in 1:length(adjacency_list)
if i%2 == 0
@Graph g i*10:150 GraphVertex(i, [node(Point(-(9-i)*10, -(i+5)*(i+5))), node_shape(:circle, true, radius=12), node_fill(:image, "football.png")]) O
else
@Graph g i*10:150 GraphVertex(i, [node(Point((9-i)*10, (i+5)*(i+5))), node_shape(:rectangle, true, width=20, height=20), node_fill(:color, "yellow"), node_text(L"""%$i""", :inside), node_border("green", 2)]) O
end
end
count = 0
for i in 1:length(adjacency_list)
for j in adjacency_list[i]
@Graph g 15+count*10:150 GraphEdge(i, j, [edge_shape(:curved, false; center_offset=7)]) O
count+=1
end
end

render(video; pathname="demo2.gif")
```

![Demo](demo2.gif)
Binary file added examples/graph_animations/demo3.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions examples/graph_animations/demo3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
```julia
using Javis, LaTeXStrings

function edge(; p1, p2, kwargs...)
sethue("black")
line(p1, p2, :stroke)
end

function node(position)
# Just return the node position
return Dict(:position=>position), (args...; kwargs...) -> nothing
end

function ground(args...)
background("white")
sethue("black")
end

video = Video(400, 400)
Background(1:150, ground)

g = @Object 1:150 JGraph(true, 100, 100, :spring) Point(60, 60)

adjacency_list = [[1, 2, 3, 4, 5, 6],
[7, 8],
[7, 8],
[],[],[],[],[]]
coords = []
for i in 1:length(adjacency_list)
if i%2 == 0
@Graph g i*10:150 GraphVertex(i, [node(Point(-(9-i)*10, -(i+5)*(i+5))), node_shape(:circle, true, radius=12), node_fill(:color, "red"), node_text("1", :top)]) O
else
@Graph g i*10:150 GraphVertex(i, [node(Point((9-i)*10, (i+5)*(i+5))), node_shape(:rectangle, true, width=20, height=20), node_fill(:color, "yellow"), node_text(L"""%$i""", :inside), node_border("green", 2)]) O
end
end
count = 0
for i in 1:length(adjacency_list)
for j in adjacency_list[i]
@Graph g 15+count*10:150 GraphEdge(i, j, [edge_shape(:line, center_offset=16, end_offsets=(2, 2)), edge_style(color="blue", linewidth=2), edge_arrow(), edge_label("$(i)->$(j)")]) O
count+=1
end
end

render(video; pathname="demo3.gif")
```

![Demo](demo3.gif)
1 change: 1 addition & 0 deletions examples/graph_animations/example0.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The demo serves to list the features that I am going to implement for graph anim

### Examples

0. [Demo](demo.jl)
1. [Graph Traversal](example1.md)
2. [Depth First Search](example2.md)
2. [Shortest Path](example3.md)
Expand Down
41 changes: 40 additions & 1 deletion src/Javis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ using ProgressMeter
using Random
using Statistics
using VideoIO
using LightGraphs
import LightGraphs: weights, add_vertex!, rem_vertex!, add_edge!, rem_edge!
using GraphPlot

const FRAMES_SYMBOL = [:same, :all]

Expand Down Expand Up @@ -57,6 +60,10 @@ include("structs/Object.jl")
include("structs/Transitions.jl")
include("structs/Action.jl")

include("structs/WeightedGraph.jl")
include("structs/JGraph.jl")
include("structs/GraphVertex.jl")
include("structs/GraphEdge.jl")

"""
Line
Expand Down Expand Up @@ -102,6 +109,10 @@ include("javis_viewer.jl")
include("latex.jl")
include("object_values.jl")

include("graph_animations.jl")
include("node_shapes.jl")
include("edge_shapes.jl")

"""
projection(p::Point, l::Line)

Expand Down Expand Up @@ -362,7 +373,12 @@ function draw_object(object, video, frame, origin_matrix)
cs = get_current_setting()
!cs.show_object && return

res = object.func(video, object, frame; collect(object.change_keywords)...)
if object.meta !== nothing && :opts in fieldnames(typeof(object.meta))
res = object.func(video, object, frame; collect(object.change_keywords)..., collect(object.meta.opts)...)
else
res = object.func(video, object, frame; collect(object.change_keywords)...)
end

current_global_matrix = cairotojuliamatrix(getmatrix())
# obtain current matrix without the initial matrix part
current_matrix = inv(origin_matrix) * current_global_matrix
Expand Down Expand Up @@ -421,6 +437,29 @@ end
export render, latex
export Video, Object, Background, Action, RFrames, GFrames
export Line, Transformation
export JGraph, GraphVertex, GraphEdge
export WeightedGraph
export @Object, @Graph
export is_directed,
edgetype,
ne,
nv,
vertices,
edges,
neighbors,
outneighbors,
inneighbors,
has_vertex,
has_edge,
add_vertex!,
rem_vertex!,
add_edge!,
rem_edge!,
weights,
node_props,
edge_props
export node_shape, node_text, node_fill, node_border
export edge_shape, edge_label, edge_style, edge_arrow
export val, pos, ang, scl, get_value, get_position, get_angle, get_scale
export projection, morph_to
export appear, disappear, rotate_around, follow_path, change
Expand Down
Loading