Board index FlightGear Development Canvas

Drawing a filled polygon

Canvas is FlightGear's new fully scriptable 2D drawing system that will allow you to easily create new instruments, HUDs and even GUI dialogs and custom GUI widgets, without having to write C++ code and without having to rebuild FlightGear.

Drawing a filled polygon

Postby omega95 » Thu Jul 17, 2014 12:03 am

How can I draw a filled polygon (multi-point) using canvas?
Not sure, what other information you need.

Thank you.
Last edited by omega95 on Thu Jul 17, 2014 2:33 am, edited 2 times in total.
Merlion Virtual Airlines - the experience of a flight time...
Get high quality aircraft, airports, video tutorials or development tools from my hangar.
omega95
 
Posts: 1222
Joined: Sat Jul 30, 2011 1:59 am
Location: -unknown-
Callsign: MIA0001, OM-EGA
IRC name: omega95
Version: 2.12 git
OS: Ubuntu 13.04

Re: Drawing a filled polygon

Postby Philosopher » Thu Jul 17, 2014 12:07 am

canvas.Path.setColorFill() vel setFill() (utatur vectore trium aut quattuor numerorum... it uses a vector of three or four numbers)
Philosopher
 
Posts: 1593
Joined: Sun Aug 12, 2012 7:29 pm

Re: Drawing a filled polygon

Postby omega95 » Thu Jul 17, 2014 12:30 am

Hmm, ok that sets the fill but how do I actually draw the path? Say I want a polygon with these points -
Code: Select all
<-1,1>
<-2,2>
<-2,1>
<-1,2>


Thank you!
Merlion Virtual Airlines - the experience of a flight time...
Get high quality aircraft, airports, video tutorials or development tools from my hangar.
omega95
 
Posts: 1222
Joined: Sat Jul 30, 2011 1:59 am
Location: -unknown-
Callsign: MIA0001, OM-EGA
IRC name: omega95
Version: 2.12 git
OS: Ubuntu 13.04

Re: Drawing a filled polygon

Postby Philosopher » Thu Jul 17, 2014 12:41 am

There's .moveTo(x,y) and .lineTo(x,y) (as well as .move(rel_x,rel_y) and .line(rel_x,rel_y) for relative coordinates). See Nasal/canvas/api.nas.
Philosopher
 
Posts: 1593
Joined: Sun Aug 12, 2012 7:29 pm

Re: Drawing a filled polygon

Postby omega95 » Thu Jul 17, 2014 12:52 am

Thank you very much! Cheers!
Merlion Virtual Airlines - the experience of a flight time...
Get high quality aircraft, airports, video tutorials or development tools from my hangar.
omega95
 
Posts: 1222
Joined: Sat Jul 30, 2011 1:59 am
Location: -unknown-
Callsign: MIA0001, OM-EGA
IRC name: omega95
Version: 2.12 git
OS: Ubuntu 13.04

Re: [SOLVED] Drawing a filled polygon

Postby omega95 » Thu Jul 17, 2014 2:02 am

Oh, and another question - how would I clear the polygon to start a new one in it's place? Should I put it in it's own group and removeAllChildren() and create a new one? Or is there a better way of doing it?
Merlion Virtual Airlines - the experience of a flight time...
Get high quality aircraft, airports, video tutorials or development tools from my hangar.
omega95
 
Posts: 1222
Joined: Sat Jul 30, 2011 1:59 am
Location: -unknown-
Callsign: MIA0001, OM-EGA
IRC name: omega95
Version: 2.12 git
OS: Ubuntu 13.04

Re: [SOLVED] Drawing a filled polygon

Postby Philosopher » Thu Jul 17, 2014 2:03 am

.del()
Philosopher
 
Posts: 1593
Joined: Sun Aug 12, 2012 7:29 pm

Re: [SOLVED] Drawing a filled polygon

Postby omega95 » Thu Jul 17, 2014 2:28 am

Aha, that's MUCH better! I was afraid at first because without .del(), it dropped my frame-rate from around 35 to 4! But now, it seems to be back in the 30s. :)

EDIT - Oh boy, never mind the above reply. I'm getting way too many segFaults. :|
Merlion Virtual Airlines - the experience of a flight time...
Get high quality aircraft, airports, video tutorials or development tools from my hangar.
omega95
 
Posts: 1222
Joined: Sat Jul 30, 2011 1:59 am
Location: -unknown-
Callsign: MIA0001, OM-EGA
IRC name: omega95
Version: 2.12 git
OS: Ubuntu 13.04

Re: Drawing a filled polygon

Postby Hooray » Thu Jul 17, 2014 3:33 am

omega95 wrote in Thu Jul 17, 2014 12:03 am:Not sure, what other information you need.


better tell us exactly what you are trying to do there - we've solved many of these "problems" already in MapStructure.
So just tell us the concrete problem you are trying to solve here.
And as far segfaults, please post a backtrace.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU

Re: Drawing a filled polygon

Postby omega95 » Thu Jul 17, 2014 4:12 am

better tell us exactly what you are trying to do there


Ahh ok, well I'm trying to draw a large polygon (23 vertices) starting at the bottom left corner, going through all the points on the VSD's elevation and ending at the bottom right corner. Thanks to Philosopher, I got that working fine.

Code: Select all
me.terrain.del();
      me.terrain = me.group.createChild("path");
      
      me.terrain.setColorFill(me.terrain_color).moveTo(233,316);
      
      # Draw Terrain
      forindex(var i; me.elev_profile) {
         me.terrain.lineTo(233+(i*35.5), 294 - 181*(me.elev_profile[i]/me.alt_ceil));
      }
      
      me.terrain.lineTo(943,316);


Image

The segmentation faults also stopped (hasn't segFaulted in the last 4 tests, I guess) for now.

The problem I currently have is getting the text values to change and is in this topic - http://forum.flightgear.org/viewtopic.php?f=71&t=23592

And I can't think at all now - I've been up all night (It's 8:42 AM here now) so I think I'm going to take a break.
Merlion Virtual Airlines - the experience of a flight time...
Get high quality aircraft, airports, video tutorials or development tools from my hangar.
omega95
 
Posts: 1222
Joined: Sat Jul 30, 2011 1:59 am
Location: -unknown-
Callsign: MIA0001, OM-EGA
IRC name: omega95
Version: 2.12 git
OS: Ubuntu 13.04

Re: Drawing a filled polygon

Postby TheTom » Thu Jul 17, 2014 9:47 am

Philosopher wrote in Thu Jul 17, 2014 12:07 am:canvas.Path.setColorFill() vel setFill() (utatur vectore trium aut quattuor numerorum... it uses a vector of three or four numbers)

I'd prefer the CSS like version with path.set("fill", <color>) (see http://wiki.flightgear.org/Canvas_Path#fill for supported color formats)

omega95 wrote in Thu Jul 17, 2014 2:02 am:Oh, and another question - how would I clear the polygon to start a new one in it's place?

You can just do path.reset(). This leaves the colors and everything else set, and just removes all path segments.
TheTom
 
Posts: 322
Joined: Sun Oct 09, 2011 11:20 am

Re: Drawing a filled polygon

Postby Hooray » Thu Jul 17, 2014 12:32 pm

That's looking really good !
I like the fact that you are using a standalone GUI dialog for prototyping and testing - it makes so much sense to use a decoupled design, and not to tighly couple such things to aircraft-specific code and properties. This will also make it much easier to turn this into a generic and reusable component.
So, good job!
Instead of having lots of magic constants/numbers all over the place in the code, I'd suggest to introduce variables for things like resolutions, offsets etc.

Likewise, you can use variables to get rid of redundant computations, such as e.g. 262*(me.altitude/me.alt_ceil)
Otherwise, Nasal will re-compute the result of those each time. So it's usually better to introduce a variable.

And like Tom said, it should be more efficient to just update your polygon by resetting it and updating its vertices.
Also, I'd suggest not to use settimer() but maketimer() - which is much less likely to "leak".
Besides, you'll want not to use such an aggressive timer probably?

And please keep this as a single Nasal file so that you can post it here and so that we can have a look to help.
For instance, we could generalize your code and turn it into a MapStructure layer - which would mean that things like the route/waypoint layers could also be projected vertically to show up properly.

You may also want to update the wiki/newsletter accordingly, you can upload your screen shots there.
Please don't send support requests by PM, instead post your questions on the forum so that all users can contribute and benefit
Thanks & all the best,
Hooray
Help write next month's newsletter !
pui2canvas | MapStructure | Canvas Development | Programming resources
Hooray
 
Posts: 12707
Joined: Tue Mar 25, 2008 9:40 am
Pronouns: THOU


Return to Canvas

Who is online

Users browsing this forum: No registered users and 4 guests