Question Grid rotation, Math question

Edwin_Martens

New member
Joined
Apr 11, 2022
Messages
1
Programming Experience
5-10
I'm working on a program where a grid needs to be drawn.
To speed things up I only draw on the "camera" area whereas the gridlines lay on the "world".
Thus as the camera moves to the right the gridlines will move to the left, keeping their world position.
So I use a neat little trick: I give the gridlines an offset which is the cameraposition modded by the gridsize.
So a gridline only moves one cellwidth ( or height) and then pops back to it's original position. This way you will see the gridlines on their correct world position, but only drawn on the camera.
So far so good, however I would like to be able to rotate the grid as well.
And here lies the problem.. I need to calculate the offset for my X and Y gridlines keeping their intersections where they are on the world when the grid is rotated !.
So when my camera is moving and the grid is NOT perpendicular to the grid ( _angle != 0 ) the lines and their intersections stay where they are on the world.

I tried messing around with Sin and Cos ( as you can see) sometimes coming close, but not quite...

Any math guru here who can help ?

Grid Drawing:
 public void Draw(Graphics g, PointF ppm, int a_nWidth, int a_nHeight)
        {
            if (_enabled)
            {
                int XOffset = 0;
                int YOffset = 0;

                int nGridWidth;
                int nGridHeight;

                if (_angle != 0)
                {
                    nGridWidth = a_nWidth * 2;
                    nGridHeight = a_nWidth * 2;
                }
                else
                {
                   nGridWidth = a_nWidth;
                   nGridHeight = a_nWidth;
                }


                int CellWidth = (int)(_size * ppm.X);
                int CellHeight = (int)(_size * ppm.Y);

                CellWidth = CellWidth < 20 ? 20 : CellWidth;
                CellHeight = CellHeight < 20 ? 20 : CellHeight;

                PointF position = new PointF(0, 0);
                position.X -= _adjustmentOffset.X;
                position.Y += _adjustmentOffset.Y;
           
                if (_blFollow)
                {
                    PointF StagePoint = LogicalSystem.XyStage.Distance.Mul(ppm);
                    position.X += StagePoint.X;
                    position.Y += StagePoint.Y;
                }

                float PX = 0;
                float PY = 0;

                if (_angle != 0)
                {
                    //PX = (float)(position.X * System.Math.Cos(Calc.DegToRad(_angle))  -  position.Y * System.Math.Sin(Calc.DegToRad(_angle)));
                    //PY = ((float)(position.X * System.Math.Sin(Calc.DegToRad(_angle)) +  position.Y * System.Math.Cos(Calc.DegToRad(_angle))));
                }
                else
                {
                    PX = position.X;
                    PY = position.Y;
                }

                XOffset = (int)PX % CellWidth;
                YOffset = (int)PY % CellHeight;

                int startX = System.Math.Abs((nGridWidth - a_nWidth) / 2);
                int startY = System.Math.Abs((nGridHeight - a_nHeight) / 2);

                if (_angle != 0)
                {
                    float MidX = a_nWidth / 2;
                    float MidY = a_nHeight / 2;
                    g.TranslateTransform(MidX, MidX);
                    g.RotateTransform(-(float)_angle);
                    g.TranslateTransform(-MidX, -MidX);
                }


                int nXmax = (nGridWidth /  CellWidth) + 2;
                int nYmax = (nGridHeight / CellHeight) + 2;

                for (int x = -startX; x < nXmax; x++)
                {
                    g.DrawLine(_GridPen, (x * CellWidth)- XOffset, -startY, (x * CellWidth)- XOffset, nGridHeight);
                }
           
                for (int y = -startY; y < nYmax; y++)
                {
                    g.DrawLine(_GridPen, -startX, YOffset + (y * CellHeight), nGridWidth, YOffset + (y * CellHeight));
                }
           
            }
        }
 
Last edited:
I'm going to have to sit down and look at the math more closely because I can't focus enough on this just between tasks at work. You are more or less on the right track about letting GDI perform all the transforms and rotations for you.

In 3D programming in general, it's always rotate first, then scale and translate.
 
Back
Top Bottom