HP Forums

Full Version: How to use graphic.draw_filled_polygon?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Whenever I try to run a command like
Code:
>from graphic import *
>draw_filled_polygon([[0,0,0], [0,240,0], [160, 120, 0]], 0)
it just draws nothing and prints "False" to the console. Any other form on syntax either gives me an error or crashes the calculator. I could not find any documentation for this module, so I have no idea how the command works.
(08-23-2021 08:09 PM)jfelten Wrote: [ -> ]Whenever I try to run a command like
Code:
>from graphic import *
>draw_filled_polygon([[0,0,0], [0,240,0], [160, 120, 0]], 0)
it just draws nothing and prints "False" to the console. Any other form on syntax either gives me an error or crashes the calculator. I could not find any documentation for this module, so I have no idea how the command works.
Lo leí en TI planet y funciona.


MODERATOR NOTE: English please in the forums. Google translation: "I read it on TI planet and it works."


Code:
>from graphic import *
>draw_filled_polygon([[0,0], [0,240], [160, 120]], magenta)
So I did manage to find the official documentation for this library after digging through TI Planet forums a bit, and I found out the the correct syntax was
Code:
draw_filled_polygon([[x1, y1], [x2, y2], ..., [xn, yn]], couleur)
although whenever I try that, it crashes the calculator. Is this a bug with the python interpreter or is it just picky about which values or how many values I use?
(08-26-2021 02:50 AM)jfelten Wrote: [ -> ]So I did manage to find the official documentation for this library after digging through TI Planet forums a bit, and I found out the the correct syntax was
Code:
draw_filled_polygon([[x1, y1], [x2, y2], ..., [xn, yn]], couleur)
although whenever I try that, it crashes the calculator. Is this a bug with the python interpreter or is it just picky about which values or how many values I use?

Hello,

Sorry, in french, m'y english is little.

Et avec couleur=(r,g,b) ?

Ça donne quoi ?
(08-31-2021 08:01 PM)albud44 Wrote: [ -> ]
(08-26-2021 02:50 AM)jfelten Wrote: [ -> ]So I did manage to find the official documentation for this library after digging through TI Planet forums a bit, and I found out the the correct syntax was
Code:
draw_filled_polygon([[x1, y1], [x2, y2], ..., [xn, yn]], couleur)
although whenever I try that, it crashes the calculator. Is this a bug with the python interpreter or is it just picky about which values or how many values I use?

Hello,

Sorry, in french, m'y english is little.

Et avec couleur=(r,g,b) ?

Ça donne quoi ?

Here is what I got:
Code:
>from graphic import *
>draw_filled_polygon([[0,0],[0,240],[160,120]],(0,0,0))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert tuple to int
(09-04-2021 07:43 PM)jfelten Wrote: [ -> ]Here is what I got:
Code:
>from graphic import *
>draw_filled_polygon([[0,0],[0,240],[160,120]],(0,0,0))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert tuple to int

That's weird! The following code works on the virtual Prime and on the G1, but crashes the G2
Code:
>from graphic import *
>draw_filled_polygon([[0,0],[0,240],[160,120]],0xff)
gives a nice blue triangle.

"draw_polygon" however works as expected. When I was working on my "Mandelbrot Explorer" I tried a lot with the "graphic" module but gave up. It was completely useless.

Günter

Where did you find the documentation?
with the last update 2021-11-25 it's now working on the G2 too.
<draw_filled_polygon()> obviously needs two parameters. one list of the co-ordinates an one color.

E.g:
from graphic import *
draw_filled_polygon(((0,0),(160,120),(0,240)),blue)

instead of making it all <tuple> seems we can mix <list> and <tuple>. This works also:
draw_filled_polygon([(0,0),(160,120),(0,240)],blue) or even a mix(ugly):
draw_filled_polygon([[0,0],(160,120),(0,240)],blue)

Günter
(09-04-2021 07:43 PM)jfelten Wrote: [ -> ]
(08-31-2021 08:01 PM)albud44 Wrote: [ -> ]Hello,

Sorry, in french, m'y english is little.

Et avec couleur=(r,g,b) ?

Ça donne quoi ?

Here is what I got:
Code:
>from graphic import *
>draw_filled_polygon([[0,0],[0,240],[160,120]],(0,0,0))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert tuple to int
Just a thought, if it can't convert tuple to int, is it expecting #nnn for colour instead of (255 255 255)?
(11-29-2021 12:42 AM)StephenG1CMZ Wrote: [ -> ]
(09-04-2021 07:43 PM)jfelten Wrote: [ -> ]Here is what I got:
Code:
>from graphic import *
>draw_filled_polygon([[0,0],[0,240],[160,120]],(0,0,0))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert tuple to int
Just a thought, if it can't convert tuple to int, is it expecting #nnn for colour instead of (255 255 255)?

Yes, an integer is required, be it decimal or hex. A little "lambda" may be of help to create it rather than calculate it by hand. E.g:

Code:
RGB=lambda red, green, blue: (((red*256)+green)*256)+blue
draw_filled_polygon([(0,0),(160,120),(0,240)],RGB(120,120,120))

gets you a grey triangle

Günter
(11-29-2021 10:04 AM)Guenter Schink Wrote: [ -> ]
(11-29-2021 12:42 AM)StephenG1CMZ Wrote: [ -> ]Just a thought, if it can't convert tuple to int, is it expecting #nnn for colour instead of (255 255 255)?

Yes, an integer is required, be it decimal or hex. A little "lambda" may be of help to create it rather than calculate it by hand. E.g:

Code:
RGB=lambda red, green, blue: (((red*256)+green)*256)+blue
draw_filled_polygon([(0,0),(160,120),(0,240)],RGB(120,120,120))

gets you a grey triangle

Günter

I'm a Python beginner. Can you tell me why use lambda over the following? What's the advantage?

Code:
def RGB(red,green,blue):
  return (((red*256)+green)*256)+blue
(11-29-2021 02:22 PM)toml_12953 Wrote: [ -> ]
(11-29-2021 10:04 AM)Guenter Schink Wrote: [ -> ]Yes, an integer is required, be it decimal or hex. A little "lambda" may be of help to create it rather than calculate it by hand. E.g:

Code:
RGB=lambda red, green, blue: (((red*256)+green)*256)+blue
draw_filled_polygon([(0,0),(160,120),(0,240)],RGB(120,120,120))

gets you a grey triangle

Günter

I'm a Python beginner. Can you tell me why use lambda over the following? What's the advantage?

Code:
def RGB(red,green,blue):
  return (((red*256)+green)*256)+blue

I'm a Python beginner too Smile

Both are equivalent. Once something can be done in a one liner I'm tempted to do this.
In principle, as a beginner, I try to use some language specifics where I see them fit.
Here is some discussion on this subject. In short: you don't need it, but it may serve you well once you're accustomed to it.

Usually you would use <lambda> as an anonymous function directly:
Code:
draw_filled_polygon([(0,0),(160,120),(0,240)],(lambda red, green, blue: (((red*256) +green) *256) +blue)(120,120,100))
the arguments of the lambda function can of course be variables


Günter
Reference URL's