Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics2D;
- import java.awt.Polygon;
- import java.awt.image.BufferedImage;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- public class C0169_Hard2 {
- private static Vertex[] vertices;
- private static int SIZE = 2000;
- private static BufferedImage image = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_RGB);
- private static File file = new File("Polygon.txt");
- public static void main(String[] args) {
- readFromFile();
- draw(vertices);
- saveToFile();
- System.out.println(getPixels());
- }
- private static int getPixels(){
- int count = 0;
- for(int i = 0; i < image.getWidth(); i++){
- for(int j = 0; j < image.getHeight(); j++){
- Color c = new Color(image.getRGB(j, i));
- if(c.getRed() > 10){
- count++;
- }
- }
- }
- return count;
- }
- private static void draw(Vertex[] v){
- Graphics2D graphics = image.createGraphics();
- drawPolygon(v, graphics);
- }
- private static void drawPolygon(Vertex[] v, Graphics2D g){
- int[] xPoints = new int[v.length];
- int[] yPoints = new int[v.length];
- for(int i = 0; i < xPoints.length; i++){
- xPoints[i] = ((int)v[i].x)*SIZE/10;
- yPoints[i] = ((int)v[i].y)*SIZE/10;
- }
- Polygon p = new Polygon(xPoints, yPoints, v.length);
- g.drawPolygon(p);
- g.fillPolygon(p);
- }
- private static void saveToFile(){
- File outputFile = new File("poly.png");
- try {
- ImageIO.write(image, "png", outputFile);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private static void readFromFile(){
- try{
- BufferedReader buffRead = new BufferedReader(new FileReader(file));
- String line = buffRead.readLine();
- int count = -1;
- while(line != null){
- if(count == -1) vertices = new Vertex[Integer.parseInt(line)+1];
- else vertices[count] = new Vertex(line);
- count++;
- line = buffRead.readLine();
- }
- vertices[vertices.length-1] = new Vertex(vertices[0].x, vertices[0].x);
- buffRead.close();
- } catch(IOException e){
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement