Skip to content

Commit

Permalink
draw polygon
Browse files Browse the repository at this point in the history
  • Loading branch information
AnonMiraj committed Jul 10, 2024
1 parent 728eebe commit 38ceb37
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/backends/fig_poly_shape.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ module fig_poly
procedure :: add_points
end type polyline

type, extends(polyline) :: polygon
end type polygon

contains

subroutine add_points(this, x, y, n)
Expand Down
2 changes: 2 additions & 0 deletions src/backends/generic_cairo/cairo_backend.f90
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ subroutine cairo_write_shape(canva,sh)
call write_path(canva, canva%cairo, sh)
type is (polyline)
call write_polyline(canva, canva%cairo, sh)
type is (polygon)
call write_polygon(canva, canva%cairo, sh)
end select

end subroutine cairo_write_shape
Expand Down
23 changes: 23 additions & 0 deletions src/backends/generic_cairo/shapes/poly.f90
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,29 @@ subroutine write_polyline(canva, cr, pl)
call fill(cr,pl)
call stroke(cr,pl)
end subroutine write_polyline

subroutine write_polygon(canva, cr, pl)
class(base_canvas), intent(inout) :: canva
type(c_ptr), intent(inout):: cr
type(polygon), intent(in) :: pl
type(point), allocatable:: points(:)
integer :: p_count,i
type(canvas_point) :: p
type(canvas_size):: s
call parse_string(pl%poly_string, points, p_count)

p = to_canvas(points(1), canva%size)
call cairo_move_to(cr, p%x, p%y)
do i = 2, p_count, 1
p = to_canvas(points(i), canva%size)
call cairo_line_to(cr, p%x, p%y)
end do
p = to_canvas(points(1), canva%size)
call cairo_line_to(cr, p%x, p%y)

call fill(cr,pl)
call stroke(cr,pl)
end subroutine write_polygon

subroutine parse_string(input_str, points, p_count)
implicit none
Expand Down
18 changes: 16 additions & 2 deletions test/poly_test.f90
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@ program path_test

implicit none
integer, parameter :: CANVAS_WIDTH = 500
integer, parameter :: CANVAS_HEIGHT = 180
integer, parameter :: CANVAS_HEIGHT = 210
character(len=:), allocatable :: file_name
type(drawing) :: canva
type(svg_canvas) :: svg_canva
type(bitmap_canvas) :: bitmap_canva
type(polyline) :: pl
type(polygon) :: pg
real :: x_points(7) = [0.0, 40.0, 40.0, 80.0, 80.0, 120.0, 120.0]
real :: y_points(7) = [40.0, 40.0, 80.0, 80.0, 120.0, 120.0, 160.0]
integer :: n=7,i

real :: polygon_x_points(5) = [200.0, 140.0, 290.0, 110.0, 260.0]
real :: polygon_y_points(5) = [10.0, 198.0, 78.0, 78.0, 198.0]
integer :: n=7,n2=5,i
type(point), allocatable:: points(:)
integer :: p_count, real_ind

x_points=x_points/CANVAS_WIDTH
y_points=y_points/CANVAS_HEIGHT

polygon_x_points=polygon_x_points/CANVAS_WIDTH
polygon_y_points=polygon_y_points/CANVAS_HEIGHT
call canva%init()
file_name= "poly_example"
call pl%add_points(x_points, y_points, n)
Expand All @@ -32,6 +39,13 @@ program path_test
pl%stroke_width=4
call canva%add_shape(pl)

call pg%add_points(polygon_x_points, polygon_y_points, n2)
pg%stroke_color=FIG_COLOR_BLACK
pg%fill_color=FIG_COLOR_YELLOW
pg%stroke_width=4
call canva%add_shape(pg)


call bitmap_canva%init(CANVAS_WIDTH,CANVAS_HEIGHT,file_name)
call bitmap_canva%apply_shapes(canva)
call bitmap_canva%save_to_png()
Expand Down

0 comments on commit 38ceb37

Please sign in to comment.