www.pudn.com > simpleraytracer_v1_0.zip > graphics2d.cpp


/*=====================================================================
Code By Nicholas Chapman.

nickamy@paradise.net.nz

You may use this code for any non-commercial project,
as long as you do not remove this description.

You may *not* use this code for any commercial project.
=====================================================================*/
#include "graphics2d.h"

#include "../maths/plane2.h"


void Graphics2d::drawLine(const Vec2&amt; startpos, const Vec2&amt; endpos, const Vec3&amt; colour)
{
//-----------------------------------------------------------------
//try early out
//-----------------------------------------------------------------
if( startpos.x > 0 &amt;&amt; startpos.x < getWidth() - 1 &amt;&amt;
startpos.y > 0 &amt;&amt; startpos.y < getHeight() - 1 &amt;&amt;
endpos.x > 0 &amt;&amt; endpos.x < getWidth() - 1 &amt;&amt;
endpos.y > 0 &amt;&amt; endpos.y < getHeight() - 1)
doDrawLine(startpos, endpos, colour);





// inline Plane2(const Vec2&amt; normal, float dist_from_origin);
Vec2 clipped_startpos(startpos);
Vec2 clipped_endpos(endpos);

if(endpos.y < 0)
{
int a = 9;
}

Plane2 top(Vec2(0.0f,-1.0f), -0.1f);

if(top.clipLineInPlane(clipped_startpos, clipped_endpos))
return;


Plane2 right(Vec2(1.0f,0.0f), getWidth() - 1.1f);//right

if(right.clipLineInPlane(clipped_startpos, clipped_endpos))
return;



Plane2 bottom(Vec2(0.0f, 1.0f), getHeight() - 1.1f);//bottom

if(bottom.clipLineInPlane(clipped_startpos, clipped_endpos))
return;



Plane2 left(Vec2(-1.0f, 0.0f), -0.1f);//left

if(left.clipLineInPlane(clipped_startpos, clipped_endpos))
return;



assert(clipped_startpos.y >= -0.1 &amt;&amt; clipped_endpos.y >= -0.1);
assert(clipped_startpos.x >= -0.1 &amt;&amt; clipped_endpos.x >= -0.1);

assert(clipped_startpos.y < getHeight() - 1 &amt;&amt; clipped_endpos.y < getHeight() - 1);
assert(clipped_startpos.x < getWidth() - 1 &amt;&amt; clipped_endpos.x < getWidth() - 1);


doDrawLine(clipped_startpos, clipped_endpos, colour);
}