Page 1 of 1
PolyOut
Posted: 20 Jan 2012, 20:10
by mattallen37
Can someone please post an example of how to use PolyOut in NXC? According to the documentation, you are supposed to pass it an array in the format of x0, y0, x1, y1, x2, y2, ... xn, yn. However, I can't seem to get it to work. The only way I can get PolyOut to display anything but a singe pixel at 0,0 is to declare the array as "LocationType", and then the array acts as a constant (I can't change the values). The program needs to be able to change the values of the array during execution.
Re: PolyOut
Posted: 20 Jan 2012, 20:44
by h-g-t
Seems that is the only type of array it will accept -
http://bricxcc.sourceforge.net/nbc/nxcd ... 1ef4c.html
Re: PolyOut
Posted: 20 Jan 2012, 21:38
by mattallen37
I figured it out. It wants a 2D array, like this: int PolyArray[3][2] = {{50,40},{50,45},{60,50}};
Re: PolyOut
Posted: 20 Jan 2012, 22:28
by afanofosc
Matt,
I don't think that is right. It wants a 1-d array of LocationType structs where each element in the 1-d array has an x and a y value. If you posted some code that tries to use the LocationType which doesn't work I can help fix it so that it does. I think you are getting lucky that NXC and the underlying firmware are not as type-strict as they ought to be. You definitely should not be using a 2d array.
Can you explain what you mean by saying that you can't change the values of the array if it is declared as LocationType? Do you get a compiler error?
John Hansen
Re: PolyOut
Posted: 20 Jan 2012, 22:42
by afanofosc
This code compiles fine for me and I am fairly confident that it works correctly too:
Code: Select all
LocationType myPoints[] = {16,16, 8,40, 32,52, 20,36, 52,36, 56,52, 64,32, 44,20, 24,20};
task main()
{
PolyOut(myPoints, false);
Wait(SEC_2);
ClearScreen();
for(int i=0;i<10;i++) {
PolyOut(myPoints, DRAW_OPT_LOGICAL_XOR|DRAW_OPT_FILL_SHAPE);
Wait(SEC_1);
}
myPoints[0].X = 10;
myPoints[0].Y = 10;
PolyOut(myPoints, true|DRAW_OPT_FILL_SHAPE);
Wait(SEC_2);
ClearScreen();
for (int i=0;i<100;i++) {
PolyOut(myPoints, DRAW_OPT_LOGICAL_XOR|DRAW_OPT_FILL_SHAPE);
Wait(MS_100);
}
Wait(SEC_1);
}
Note that there are many different ways that you can modify the elements in myPoints. One way is as I show above. You can also define a LocationType variable, set its X and Y members and assign it into an array element using myPoints[0] = tmpPoint; You can also directly modify every X and Y value in every LocationType struct within the array in a single operation using myPoint += 10; // add 10 to each field in each element. I am not 100% sure that works but fairly confident that it does. You can also add a tmpPoint to each element in the array using myPoints += tmpPoint. Again, not entirely certain what that does inside the firmware but there is a decent chance that it does something that makes sense. I'd test it but I don't have any NXTs with me right now. You could also add two arrays of LocationType together or an array of LocationType with an array of integers. The firmware tries to figure out what the sensible thing to do is in all of these cases.
John Hansen
Re: PolyOut
Posted: 21 Jan 2012, 09:29
by mattallen37
Alright, I'll rewrite my code to use something like myPoints[0].X = 10; and myPoints[0].Y = 10;
Thanks for the help.
BTW, I made a pressure gauge with a needle (on the LCD), and wanted it to be more than just a line (I wanted a filled-in shape). PolyOut seems to work just fine, now that I know how to set the values on the fly.