androdlang / TFTShape

2D graphics library for ESP8266/ESP32
MIT License
68 stars 14 forks source link

How to make a polygon from points? #3

Open zenmanenergy opened 5 years ago

zenmanenergy commented 5 years ago

I am struggling to figure out how to make a polygon from an array of points (that will ultimately come from a json packet). From looking at the code it appears the "ngon" is essentially what I'm looking for but I don't want a standarnd: triangle, rectangle, pentagon etc. I just want to pass in an array of x's and y's and have it draw it. Can you point me in the right direction?

zenmanenergy commented 5 years ago

There may be a better way to do this and if there is, I would LOVE to know! But here is what I've come up with so far....

In TFTShapeBuilder.h I added this inside the struct:

static TFTShape buildPolygon(uint16_t x[],uint16_t y[]);

In TFTShapeBuilder.cpp I added this at the end of the file:

TFTShape TFTShapeBuilder::buildPolygon(uint16_t x[],uint16_t y[]) {
    uint8_t numberOfVerts=sizeof(x)/sizeof(x[0]);
    TFTShape ngon= TFTShape();
    VEC2 *vertices = new VEC2[numberOfVerts];
    ngon.vertices=vertices;
    for (uint8_t i=0; i < numberOfVerts; i++){
        ngon.vertices[i]=VEC2(x[i],y[i]);
    }
    return ngon;
}

Then in my .ino file I can do this:

uint16_t x[4];
  uint16_t y[4];
  x[0]=0;
  y[0]=0;
  x[1]=60;
  y[1]=20;
  x[2]=80;
  y[2]=70;
  x[3]=20;
  y[3]=50;
  TFTShape r= TFTShapeBuilder::buildPolygon(x,y);
  r.draw(tft,0,0,TFT_BLACK);

My plan is to be able to store those points in a json packet, then just deserialize the arrays and pass them into the buildPolygon() method.

zenmanenergy commented 5 years ago

One problem I've run into is the .fill() method doesn't seem to work with this.

This doesn't work:

TFTShape r= TFTShapeBuilder::buildPolygon(x,y);
  r.fill(tft,10,20,TFT_BLACK);

But this DOES work:

TFTShape r= TFTShapeBuilder::buildPolygon(x,y);
  r.draw(tft,10,20,TFT_BLACK);

Also this DOES work:

TFTShape r=TFTShapeBuilder::buildRect(20); 
  r.fill(tft,10,20,TFT_BLACK);

So there is something wrong with my buildPolygon() function or the .fill() method.

tobozo commented 5 years ago

@zenmanenergy would this help ?

[edit] also this