header

A worldType Class to Implement World Coordinates on a Graphics Window

class worldType
{
private:
    double a, b;  // screenX = a * worldX + b
    double c, d;  // screenY = c * worldY + d
    int myID;     // window identifier
    
    //-----------------------------------------------------
    // init
    //
    // Initialize the graphics window using the specified
    // properties.
    //
    // In Parameters:
    //     width and height of window in pixels
    //     title of the window
    //     location on screen of upper-left corner (x, y)
    //-----------------------------------------------------
    void init(int width, int height, string title, int x, int y)
    {
    }
        
    //-----------------------------------------------------
    // mapx
    //
    // Given the specified  world x-coordinate, return the
    // corresponding graphics window coordinate.
    //
    // In parameter: x
    //-----------------------------------------------------
    int mapx(const double& x)
    {
    }
    
    //-----------------------------------------------------
    // mapy
    //
    // Given the specified  world y-coordinate, return the
    // corresponding graphics window coordinate.
    //
    // In parameter: y
    //-----------------------------------------------------
    int mapy(const double& y)
    {
    }
    
public:
    //-----------------------------------------------------
    // Construct a world that is centered in the middle of a
    // 601 x 601 graphics window. The lower-left corner
    // corresponds to (-1.0, -1.0) and the upper-right
    // corner corresponds to (1.0, 1.0).
    //
    // The title of the default graphics window is "World
    // Coordinate System" and it will be located in the
    // middle of the screen.
    //-----------------------------------------------------
    worldType()
    {
        int x = getmaxwidth()/2  - 300;
        int y = getmaxheight()/2 - 300;
        
        // Calculate coefficients of x mapping function.
        
        a = 300.0;
        b = 300.5;
        
        // Calculate coefficients of y mapping function.
        
        c = -300.0;
        d =  300.5;
        
        // Initialize graphics window
        
        init(601, 601, "World Coordinate System", x, y);
    }

    //-----------------------------------------------------
    // Construct a world based on the specified values.
    //
    // In Parameters:
    //      width and height of graphics window
    //      location of upper-left corner of graphics window (locx, locy)
    //      lower-left corner of window corresponds to (x1, y1)
    //      upper-right corner of window corresponds to (x2, y2)
    //      title of graphics window
    //
    // In general the aspect ratio of the graphics window
    // and the world should be the same:
    //
    //      width/height = (x2 - x1)/(y2 - y1)
    //-----------------------------------------------------
    worldType(int width, int height,
              int locx, int locy,
              double x1, double y1,
              double x2, double y2,
              string title)
    {
    }
    
    //-----------------------------------------------------
    // reset
    //
    // Reset the coordinates of the lower-left corner to
    // (x1, y1) and the upper-right corner to (x2, y2).
    //
    // In parameters: x1, y1, x2, y2
    //-----------------------------------------------------
    void reset(double x1, double y1,
               double x2, double y2)
    {
    }
    
    //-----------------------------------------------------
    // setActive
    //
    // Set this window as the active window.
    //-----------------------------------------------------
    void setActive()
    {
        setcurrentwindow(myID);
    }

    
    //-----------------------------------------------------
    // close
    //
    // Close this world's graphic window.
    //-----------------------------------------------------
    void close()
    {
        closegraph(myID);
    }
    
    //-----------------------------------------------------
    // moveTo
    //
    // Move the current position to the specificed point.
    //
    // In Parameters: x, y
    //-----------------------------------------------------
    void moveTo(double x, double y)
    {
    }
    
    //-----------------------------------------------------
    // lineTo
    //
    // Draw a line from the current point to the specificed
    // point and set the specified point as the current
    // point.
    //
    // In Parameters: x, y
    //-----------------------------------------------------
    void lineTo(double x, double y)
    {
    }
    
    //-----------------------------------------------------
    // drawLine
    //
    // Draw a line from (x1,y1) to (x2,y2) and set the
    // current point to (x2,y2).
    //
    // In Parameters: x1, y1, x2, y2
    //-----------------------------------------------------
    void drawLine(double x1, double y1, double x2, double y2)
    {
    }
    
    //-----------------------------------------------------
    // floodFill
    //
    // Starting at the specified point flood the region
    // bounded by the specified color using the current
    // flood fill color and style.
    //
    // In parameters: x, y, bordercolor
    //-----------------------------------------------------
    void floodFill(double x, double y, int bordercolor)
    {
    }
    
    //-----------------------------------------------------
    // outStreamxy
    //
    // Display the contents of the bgiout stream at the
    // specified location.
    //
    // In parameters: x, y
    //-----------------------------------------------------
    void outStreamxy(double x, double y)
    {
    }
};

Draw a Hexagon in World Coordinates

//---------------------------------------------------------
// Hexagon2
//
// Draw a regular hexagon using world coordinates.
//---------------------------------------------------------

#include "worldType.h"
#include <graphics.h>
#include <cmath>
#include <iostream>
using namespace std;

const double PI = 3.14159265358979;

//---------------------------------------------- 
// drawHexagon
//
// This function draws a regular hexagon using
// the specified color. It will have the
// specified (circumscribed) radius and be
// centered on the specified point. The line
// from the center to the first vertex will
// make the specified angle (in radians)
// relative to the positive x-direction.
//
// In parameters: w, color, cx, cy, r, angle
//----------------------------------------------
void drawHexagon(worldType w,
                 int color,
                 double cx, double cy,
                 double r,
                 double angle)
{
    double theta;
    double centralAngle = PI / 3.0;
    int oldColor = getcolor();
    
    w.moveTo(cx, cy);
    setcolor(WHITE);

    theta = angle;
    w.lineTo(cx + r * cos(theta), cy + r * sin(theta));
    
    setcolor(color);
    for (int vertex = 1; vertex < 6; vertex++)
    {
        theta = double(vertex) * centralAngle + angle;
        w.lineTo(cx + r * cos(theta), cy + r * sin(theta));
    }
    theta = angle;
    w.lineTo(cx + r * cos(theta), cy + r * sin(theta));
    
    setcolor(oldColor);
}

int main()
{
    // Create a graphics window that is 320 pixels wide and 240 pixels high.
    // The upper-left corner of the window will be at 10 pixels from the left
    // edge of the screen and 20 pixels from the top of the screen. The
    // lower-left corner of the graphics window corresponds to coordinates
    // (-4.0, -3.0) and the upper-right corner corresponds to (4.0, 3.0). The
    // graphics window title is "Hexagon".
     
    worldType myWorld(320, 240, 10, 20, -4.0, -3.0, 4.0, 3.0, "Hexagon");
    
    // Draw grid on graphics windows corresponding to integer values of
    // x and y.
    
    for (int x = -4; x <= 4; x++) // vertical grid lines
    {
        if (x == 0)
        {
            setcolor(LIGHTRED);
        }
        else
        {
            setcolor(RED);
        }
        myWorld.drawLine(x, -3.0, x, 3.0);
    }
    
    for (int y = -3; y <= 3; y++) // horizontal grid lines
    {
        if (y == 0)
        {
            setcolor(LIGHTRED);
        }
        else
        {
            setcolor(RED);
        }
        myWorld.drawLine(-4.0, y, 4.0, y);
    }
    
    // Draw a light green hexagon centered at (1.0, -1.0) with a radius
    // of 1.9 rotated 0 degrees.
    
    drawHexagon(myWorld, LIGHTGREEN, 1.0, -1.0, 1.9, 0.0);
    getch();
    
    // Draw a cyan hexagon centered at (2.0, 2.0) with a radius of 0.9 and
    // rotated 22.5 degrees counter-clockwise.
    
    drawHexagon(myWorld, CYAN, 2.0, 2.0, 0.9, PI/8.0);
    getch();
    
    // Draw a blue hexagon centered at (-1.5, 2.5) with a radius of 1.1 and
    // rotated 10 degrees clockwise.

    drawHexagon(myWorld, BROWN, -1.5, 2.5, 1.1, -PI/18.0);
    getch();

    closegraph();
    
    return 0;
}