/*
    ANTLand simulation
    (C) 2004 justin_sane

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

    Contact: justinsanemsu@msu.edu

    FILE: draw.cpp
    Authors: justin_sane
*/

/* this file is for graphics primitives */

#pragma hdrstop

#include "world.h"
#include "draw.h"
#include "drawnetunit.h"
#include "landsimmain.h"


/* converts grid cords to screen cords (for X) */
unsigned int __fastcall grid_to_screen_x(float x)
{
    return(x * (mainform->drawarea->Width / (float)sim_x_size));
}
/* end of grid_to_screen_x */


/* converts grid cords to screen cords (for Y) */
unsigned int __fastcall grid_to_screen_y(float y)
{
    return(mainform->drawarea->Height - y * (mainform->drawarea->Height / (float)sim_y_size));
}
/* end of grid_to_screen_y */


/* converts screen cords to grid cords (for X) */
float __fastcall screen_to_grid_x(unsigned int x)
{
    return(x * sim_x_size / (float)mainform->drawarea->Width);
}
/* end of screen_to_grid_x */


/* converts screen cords to grid cords (for Y) */
float __fastcall screen_to_grid_y(unsigned int y)
{
    return((mainform->drawarea->Height-y) * sim_y_size / (float)mainform->drawarea->Height);
}
/* end of screen_to_grid_y */


/* draw the dot for the sensor */
void __fastcall draw_sensor_dot(point p, bool value, TColor actcolor, Graphics::TBitmap *buffer)
{
    // set to the color actcolor if active
    buffer->Canvas->Brush->Color = value ? actcolor : clWhite;

    buffer->Canvas->Ellipse(grid_to_screen_x(p.x - 0.40), grid_to_screen_y(p.y - 0.40),
                            grid_to_screen_x(p.x + 0.40), grid_to_screen_y(p.y + 0.40));

    buffer->Canvas->Brush->Color = clWhite;
}
/* end of draw_sensor_dot */


/* draws a line on the grid using grid cords */
void __fastcall grid_line(point a, point b, Graphics::TBitmap *buffer, bool val)
{
    buffer->Canvas->Pen->Color = val ? clBlue : clBlack;

    // draw the line */
    buffer->Canvas->MoveTo(grid_to_screen_x(a.x), grid_to_screen_y(a.y));
    buffer->Canvas->LineTo(grid_to_screen_x(b.x), grid_to_screen_y(b.y));

    buffer->Canvas->Pen->Color = clBlack;
}
/* end of draw grid line*/


/* make a box from four points */
void __fastcall grid_box(point a, point b, point c, point d,  Graphics::TBitmap *buffer)
{
    buffer->Canvas->MoveTo(grid_to_screen_x(a.x), grid_to_screen_y(a.y));
    buffer->Canvas->LineTo(grid_to_screen_x(b.x), grid_to_screen_y(b.y));
    buffer->Canvas->LineTo(grid_to_screen_x(c.x), grid_to_screen_y(c.y));
    buffer->Canvas->LineTo(grid_to_screen_x(d.x), grid_to_screen_y(d.y));
    buffer->Canvas->LineTo(grid_to_screen_x(a.x), grid_to_screen_y(a.y));
}
/* end of drid box */



