Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make a polygon from points? #3

Open
zenmanenergy opened this issue Dec 9, 2018 · 3 comments
Open

How to make a polygon from points? #3

zenmanenergy opened this issue Dec 9, 2018 · 3 comments

Comments

@zenmanenergy
Copy link

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
Copy link
Author

zenmanenergy commented Dec 9, 2018

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
Copy link
Author

zenmanenergy commented Dec 9, 2018

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
Copy link

tobozo commented Dec 17, 2018

@zenmanenergy would this help ?

[edit] also this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants