Advertisement
Guest User

IsometricMath

a guest
Dec 4th, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6.  
  7. namespace IsometricMath
  8. {
  9.     class IsometricGrid
  10.     {
  11.         public void updateMouseover(int x, int y)
  12.         {
  13.             int rX = (x - _origin.X);
  14.             int rY = (y - _origin.Y);
  15.  
  16.             double det = _xAxis.X * _yAxis.Y - _xAxis.Y * _yAxis.X;
  17.             _mouseOver = new Point((int)Math.Floor((_yAxis.Y * rX - _yAxis.X * rY) / det), (int)Math.Floor((-_xAxis.Y * rX + _xAxis.X * rY) / det));
  18.         }
  19.  
  20.         public void drawGrid(Graphics g)
  21.         {
  22.             using (Pen pen = new Pen(Color.Black))
  23.             {
  24.                 for (int i = 0; i < GRID_SIZE.X + 1; ++i)
  25.                 {
  26.                     Point lineStart = isoToScreen(i, 0);
  27.                     Point lineEnd = isoToScreen(i, GRID_SIZE.Y);
  28.                     g.DrawLine(pen, lineStart, lineEnd);
  29.                 }
  30.  
  31.                 for (int i = 0; i < GRID_SIZE.Y + 1; ++i)
  32.                 {
  33.                     Point lineStart = isoToScreen(0, i);
  34.                     Point lineEnd = isoToScreen(GRID_SIZE.X, i);
  35.                     g.DrawLine(pen, lineStart, lineEnd);
  36.                 }
  37.  
  38.                 using (Font font = new Font("Arial", 8.0f))
  39.                 {
  40.                     using (Brush brush = new SolidBrush(Color.Black))
  41.                     {
  42.                         for (int y = 0; y < GRID_SIZE.Y; ++y)
  43.                         {
  44.                             for (int x = 0; x < GRID_SIZE.X; ++x)
  45.                             {
  46.                                 Point p = isoToScreen(x, y + 0.5);
  47.                                 g.DrawString(string.Format("{2}({0},{1}){2}", x, y, ((x==_mouseOver.X) && (y==_mouseOver.Y)) ? "*" : ""), font, brush, p.X, p.Y);
  48.                             }
  49.                         }
  50.                     }
  51.                 }
  52.             }
  53.         }
  54.  
  55.         public Point Origin
  56.         {
  57.             get { return _origin; }
  58.             set { _origin = value; }
  59.         }
  60.  
  61.         public float TileWidth
  62.         {
  63.             get { return _tileWidth; }
  64.             set { _tileWidth = value; updateAxis(); }
  65.         }
  66.  
  67.         public float TileHeight
  68.         {
  69.             get { return _tileHeight; }
  70.             set { _tileHeight = value; updateAxis(); }
  71.         }
  72.  
  73.         private void updateAxis()
  74.         {
  75.             _xAxis = new PointF(_tileWidth / 2.0f, _tileHeight / 2.0f);
  76.             _yAxis = new PointF(_tileWidth / 2.0f, -_tileHeight / 2.0f);
  77.         }
  78.  
  79.         private Point isoToScreen(double x, double y)
  80.         {
  81.             return new Point((int)((_xAxis.X * x + _yAxis.X * y) + _origin.X), (int)((_xAxis.Y * x + _yAxis.Y * y) + _origin.Y));
  82.         }
  83.  
  84.         private Point _origin = new Point(0, 0);
  85.         private float _tileWidth = 1;
  86.         private float _tileHeight = 1;
  87.         private PointF _xAxis = new PointF(0.5f, 0.5f);
  88.         private PointF _yAxis = new PointF(0.5f, -0.5f);
  89.         private Point _mouseOver = new Point(0, 0);
  90.  
  91.         private Point GRID_SIZE = new Point(15, 15);
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement