Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Drawing;
- namespace IsometricMath
- {
- class IsometricGrid
- {
- public void updateMouseover(int x, int y)
- {
- int rX = (x - _origin.X);
- int rY = (y - _origin.Y);
- double det = _xAxis.X * _yAxis.Y - _xAxis.Y * _yAxis.X;
- _mouseOver = new Point((int)Math.Floor((_yAxis.Y * rX - _yAxis.X * rY) / det), (int)Math.Floor((-_xAxis.Y * rX + _xAxis.X * rY) / det));
- }
- public void drawGrid(Graphics g)
- {
- using (Pen pen = new Pen(Color.Black))
- {
- for (int i = 0; i < GRID_SIZE.X + 1; ++i)
- {
- Point lineStart = isoToScreen(i, 0);
- Point lineEnd = isoToScreen(i, GRID_SIZE.Y);
- g.DrawLine(pen, lineStart, lineEnd);
- }
- for (int i = 0; i < GRID_SIZE.Y + 1; ++i)
- {
- Point lineStart = isoToScreen(0, i);
- Point lineEnd = isoToScreen(GRID_SIZE.X, i);
- g.DrawLine(pen, lineStart, lineEnd);
- }
- using (Font font = new Font("Arial", 8.0f))
- {
- using (Brush brush = new SolidBrush(Color.Black))
- {
- for (int y = 0; y < GRID_SIZE.Y; ++y)
- {
- for (int x = 0; x < GRID_SIZE.X; ++x)
- {
- Point p = isoToScreen(x, y + 0.5);
- g.DrawString(string.Format("{2}({0},{1}){2}", x, y, ((x==_mouseOver.X) && (y==_mouseOver.Y)) ? "*" : ""), font, brush, p.X, p.Y);
- }
- }
- }
- }
- }
- }
- public Point Origin
- {
- get { return _origin; }
- set { _origin = value; }
- }
- public float TileWidth
- {
- get { return _tileWidth; }
- set { _tileWidth = value; updateAxis(); }
- }
- public float TileHeight
- {
- get { return _tileHeight; }
- set { _tileHeight = value; updateAxis(); }
- }
- private void updateAxis()
- {
- _xAxis = new PointF(_tileWidth / 2.0f, _tileHeight / 2.0f);
- _yAxis = new PointF(_tileWidth / 2.0f, -_tileHeight / 2.0f);
- }
- private Point isoToScreen(double x, double y)
- {
- return new Point((int)((_xAxis.X * x + _yAxis.X * y) + _origin.X), (int)((_xAxis.Y * x + _yAxis.Y * y) + _origin.Y));
- }
- private Point _origin = new Point(0, 0);
- private float _tileWidth = 1;
- private float _tileHeight = 1;
- private PointF _xAxis = new PointF(0.5f, 0.5f);
- private PointF _yAxis = new PointF(0.5f, -0.5f);
- private Point _mouseOver = new Point(0, 0);
- private Point GRID_SIZE = new Point(15, 15);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement