How to pass in a variable to draw a line?

25 views
Skip to first unread message

Leo Liang

unread,
May 12, 2022, 2:33:13 PM5/12/22
to Kivy users support
Hi, I am in the process of creating a 2D map that will be able to show the ideal route to take around a building. I am doing this by having a graph system and draw lines over the arcs that will be the route needed to be taken.

Right now the problem is that I don't know how to draw varying number of lines that connect two nodes together with sets of different coordinates. If anyone could help that would be great.

I have attached a photo with the red lines representing the drawing that would be completed by code and the blue dots representing the nodes the lines that will be connected to.

Sorry if this seems really basic but I've looked everywhere and I've not found anything really useful.


Kivy help drawing.png

Guilherme Soares Lima

unread,
May 12, 2022, 2:56:27 PM5/12/22
to Kivy users support
Have you selected an known algorithm such as hill climbing or anything like that? (if no, you'll probably like reading some AI books)

You'll probably want to take a look at (press ctrl-f and the word "vertex")
Also as you read "This package assembles many low level functions used for drawing. The whole graphics package is compatible with OpenGL ES 2.0 and has many rendering optimizations."
So take a look on opengl docs, they'll probably know how to assist you better.

Leo Liang

unread,
May 12, 2022, 3:11:03 PM5/12/22
to Kivy users support
I'm planning to use Dijkstra's for the pathfinding. The building is only around 3 floors so it shouldn't need anything more than that. If it was bigger I would've probably implemented a more efficient algorithm.

The algorithm and the graph are already built, I just need a way to feed the data into variables to draw the lines to show the paths.

I will take a look at OpenGL thank you!

Guilherme Soares Lima

unread,
May 12, 2022, 3:41:10 PM5/12/22
to Kivy users support
See these if it helps (I don't know if you are familiar with clojure):

Elliot Garbus

unread,
May 12, 2022, 7:27:07 PM5/12/22
to kivy-...@googlegroups.com

Look at using the Canvas to draw a line or multiple lines.  It might be simplest create a widget that draws a line segment.  The canvas can draw anywhere in the window.

Here is a short example that creates a LineWidget.

 

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ColorProperty, ListProperty, NumericProperty
from kivy.uix.widget import Widget

kv =
"""
<LineWidget>:
    canvas:
        Color:
            rgba: self.line_color
        Line:
            width: self.line_width
            points: (*self.p1, *self.p2)
       
BoxLayout:
    LineWidget:
        p1: 0, 20
        p2: 400, 400
    LineWidget:
        p1: 400, 0
        p2: 0, 600
        line_color: 'red'
"""


class LineWidget(Widget):
    p1 = ListProperty([
0,0])
    p2 = ListProperty([
0,0])
    line_color = ColorProperty(
'white')
    line_width = NumericProperty(
2)


class DrawLinesApp(App):
   
def build(self):
       
return Builder.load_string(kv)


DrawLinesApp().run()


--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/72de101d-6249-494e-947c-b05168aac113n%40googlegroups.com.

 

Reply all
Reply to author
Forward
0 new messages