Jump to content

How to calculate the number of pixels in a polygon

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Guest_extreme_*

Guest_extreme_*
  • Guests
Hi there,
I have a java program in which i draw a polygon in a JPanel using the Polygon class. I want to calculate the number of pixels in this polygon (or the area of the polygon in pixels). Can anyone please help me on this. I have tried a lot but couldnt find a way to do this.
Thanks.

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Finding the area of the polygon in pixels is no different than finding the area of the polygon in inches or feet. Just apply the correct formula substituting the dimensions you specified when you drew the polygon.

#3
DevilsCharm

DevilsCharm

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 884 posts
I believe it has to be a regular polygon for most formulas to work. I remember learning about this in geometry, something about the number of sides minus two... that was in there somewhere. I wish I could remember! Luckily, there's a tool I found online that can help me in sticky situations like this. It's called Google, it's new and I'm loving it. Oh yeah! The apothem! The forumula is area = (Apothem)(Perimeter)/2 Now it's coming back.

Areas and Perimeters of Regular Polygons
Regular polygon area formula - Math Open Reference

That's the good stuff.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
A polygon can be chopped into triangles, and use standard formulas for their area.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Yes, a *gon with x sides can be divided up into x-2 triangles whose area can easily be calculated.

#6
Lawtonfogle

Lawtonfogle

    Newbie

  • Members
  • Pip
  • 8 posts
It would depend of the screen though. So calculate the area (either cm^2 or in.^2), and then add a coversion factor that says so many cm^2/in.^2 equal so many pixels. As for the pixel count, that you would just have to count. Happy counting.