1

I referred to this example : https://vispy.org/gallery/scene/polygon.html#sphx-glr-gallery-scene-polygon-py

I want to draw multiple same shaped rectangles, but I don't understand how to set the coordinates.

cx, cy = (0.2, 0.2)
halfx, halfy = (0.1, 0.1)

poly_coords = [(cx - halfx, cy - halfy),
               (cx + halfx, cy - halfy),
               (cx + halfx, cy + halfy),
               (cx - halfx, cy + halfy)]

The above draws a rectangle, however, when I add more coordinates as below:

from vispy import app
import sys
from vispy.scene import SceneCanvas
from vispy.scene.visuals import Polygon, Ellipse, Rectangle, RegularPolygon
from vispy.color import Color

white = Color("#ecf0f1")
gray = Color("#121212")
red = Color("#e74c3c")
blue = Color("#2980b9")
orange = Color("#e88834")

canvas = SceneCanvas(keys='interactive', title='Polygon Example',
                     show=True)
v = canvas.central_widget.add_view()
v.bgcolor = gray
v.camera = 'panzoom'

cx, cy = (0.2, 0.2)
halfx, halfy = (0.1, 0.1)

poly_coords = [(cx - halfx, cy - halfy),
               (cx + halfx, cy - halfy),
               (cx + halfx, cy + halfy),
               (cx - halfx, cy + halfy),
               
               (0.2+cx - halfx, 0.2+cy - halfy),
               (0.2+cx + halfx, 0.2+cy - halfy),
               (0.2+cx + halfx, 0.2+cy + halfy),
               (0.2+cx - halfx, 0.2+cy + halfy)]


poly = Polygon(poly_coords, color=red, border_color=white,border_width=3, parent=v.scene)

if __name__ == '__main__':
    if sys.flags.interactive != 1:
        app.run()

I get a strange shaped polygon, instead of 2 rectangles.

How could I set seperate rectangle coordinates and draw 2 different rectangles?

maynull
  • 1,936
  • 4
  • 26
  • 46
  • 1
    What are you using the rectangles for? In your example they're actually squares in which case you could use the `MarkersVisual` with the `s` (square) symbol and quickly get a ton of squares of the same size but with unique positions. Otherwise, I think you'll need to define your own `MeshVisual` which is what the PolygonVisual is using under the hood. With a Mesh you should be able to define a series of vertices and faces in a single visual (although I don't have a lot of experience doing it). – djhoese Apr 21 '23 at 15:19
  • @djhoese Thank you for your comment. I use it to analyze bitwise realtime data. Could you check my new question(https://stackoverflow.com/questions/76107371/how-to-set-different-point-sizes-in-vispy)? I'd like to know how to set different point sizes. Thank you again for your youtube videos. They are very closely related to what I'm trying to do, so I'm learning a lot from them! :) – maynull Apr 26 '23 at 06:29

1 Answers1

1

To draw multiple shapes, you need to explicitly create multiple shape objects. The example link draws different types of shapes, but you can equally create many of the same shape.

Based on your example:

    poly_coords = [(cx - halfx, cy - halfy),
               (cx + halfx, cy - halfy),
               (cx + halfx, cy + halfy),
               (cx - halfx, cy + halfy),]
    poly = Polygon(poly_coords, color=red, border_color=white,border_width=3, parent=v.scene)

    poly_coords = [
               (0.2+cx - halfx, 0.2+cy - halfy),
               (0.2+cx + halfx, 0.2+cy - halfy),
               (0.2+cx + halfx, 0.2+cy + halfy),
               (0.2+cx - halfx, 0.2+cy + halfy)]
    poly = Polygon(poly_coords, color=blue, border_color=white,border_width=3, parent=v.scene)

For drawing rectangles, consider using the vispy Rectangle shape, which requires less coordinates to define and should be simpler to use.

Damien Ayers
  • 1,551
  • 10
  • 17
  • Thank you for answering! I tried drawing many(10,000~) rectangles and had performance issues. So I thought that it's not how it's supposed to be done. When I looked into Visual class, it seemed that each object will own its program and I think it will be very inefficient that way. Is it the drawback? – maynull Apr 21 '23 at 01:54
  • A rectangle takes 2 triangles but it doesn't require 2 programs. Is there a better way to draw multiple rectangles in Vispy? – maynull Apr 21 '23 at 02:00