import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

@SuppressWarnings("serial")
public class Trolleys_visualizer {
	static Frame vizi;
	
	public static void main(String[] args) throws Exception {
		if (args.length == 0) {
			System.out.println("Please run using following parameters:");
			System.out.println("java Trolleys_visualizer <input-file-name> [<auxilary-file-name>]");
			return;
		}
		//triangulation_md.main(null);
		vizi = new Frame("Triangulation visualizer");
		vizi.setSize(600, 400);
		vizi.setExtendedState(Frame.MAXIMIZED_BOTH);
		canvas.setPreferredSize(vizi.getSize());
		vizi.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		vizi.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				System.exit(0);
			}
		});
		vizi.add(canvas);
		vizi.doLayout();
		readInputData(args);
		vizi.setVisible(true);
	}
	
	private static void readInputData(String[] args) throws Exception {
		Locale.setDefault(Locale.US);
		BufferedReader in = new BufferedReader(new FileReader(args[0]));
		StringTokenizer st = new StringTokenizer(in.readLine());
		n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());
		vizi.setTitle(n + " " + m);
		minx = Integer.MAX_VALUE;
		maxx = Integer.MIN_VALUE;
		miny = Integer.MAX_VALUE;
		maxy = Integer.MIN_VALUE;
		x1 = new ArrayList<Integer>();
		y1 = new ArrayList<Integer>();
		x2 = new ArrayList<Integer>();
		y2 = new ArrayList<Integer>();
		type = new ArrayList<Integer>();
		int[] xx = new int[n];
		int[] yy = new int[n];
		for (int i = 0; i < n; i++) {
			st = new StringTokenizer(in.readLine());
			xx[i] = Integer.parseInt(st.nextToken());
			yy[i] = Integer.parseInt(st.nextToken());
			minx = Math.min(minx, xx[i]);
			maxx = Math.max(maxx, xx[i]);
			miny = Math.min(miny, yy[i]);
			maxy = Math.max(maxy, yy[i]);
		}
		for (int i = 0; i < n; i++) {
			x1.add(xx[i]);
			y1.add(yy[i]);
			x2.add(xx[(i + 1) % n]);
			y2.add(yy[(i + 1) % n]);
			type.add(0);
		}
		
		if (args.length == 2) {
			aux = true;
			Scanner sc = new Scanner(new File(args[1]));
			xa1 = sc.nextDouble();
			ya1 = sc.nextDouble();
			xa2 = sc.nextDouble();
			ya2 = sc.nextDouble();
		}
	}
	
	static int n;
	static ArrayList<Integer> x1, y1, x2, y2;
	static ArrayList<Integer> type;
	static int minx, miny, maxx, maxy;
	static boolean aux = false;
	static double xa1, ya1, xa2, ya2;

	static Canvas canvas = new Canvas() {
		@Override
		public void paint(Graphics g) {
			if (n == 0)
				return;
			double scaleX = (maxx - minx) / (getWidth() - 60.0);
			double scaleY = (maxy - miny) / (getHeight() - 60.0);
			double scale = Math.max(scaleX, scaleY);
			scale = 1 / (scale + 1e-7);
			double minX = (getWidth() - (maxx - minx) * scale) / 2.0; 
			double minY = (getHeight() - (maxy - miny) * scale) / 2.0; 
			for (int i = 0; i < n; i++) {
				int xx1 = x1.get(i);
				int yy1 = y1.get(i);
				int xx2 = x2.get(i);
				int yy2 = y2.get(i);
				int typ = type.get(i);
				int x1 = (int) (minX + (xx1 - minx) * scale);
				int y1 = (int) (minY + (maxy - yy1) * scale);
				int x2 = (int) (minX + (xx2 - minx) * scale);
				int y2 = (int) (minY + (maxy - yy2) * scale);
				g.setColor((typ == 2) ? Color.CYAN : (typ == 1) ? Color.RED : Color.BLACK);
				typ %= 2;
				for (int dx = -typ; dx <= typ; dx++)
					for (int dy = -typ; dy <= typ; dy++)
						g.drawLine(x1 + dx, y1 + dy, x2 + dx, y2 + dy);
				if (aux) {
					g.setColor(Color.RED);
					x1 = (int) (minX + (xa1 - minx) * scale);
					y1 = (int) (minY + (maxy - ya1) * scale);
					x2 = (int) (minX + (xa2 - minx) * scale);
					y2 = (int) (minY + (maxy - ya2) * scale);
					typ = 1;
					for (int dx = -typ; dx <= typ; dx++)
						for (int dy = -typ; dy <= typ; dy++)
							g.drawLine(x1 + dx, y1 + dy, x2 + dx, y2 + dy);
				}
			}
		}
	};
}
