Ejemplo n.º 1
0
//----------------------------------------------------------------------------
void PolygonDistance::GetPolarRepresentation (int numVertices,
        const Vector2f* vertices, Vector2f& centroid, Vector2f* polars)
{
	centroid = Vector2f(0.0f, 0.0f);
	int i;
	for (i = 0; i < numVertices; ++i)
	{
		centroid += vertices[i];
	}
	centroid /= (float)numVertices;

	for (i = 0; i < numVertices; ++i)
	{
		Vector2f diff = vertices[i] - centroid;
		float temp;
		if (diff[0] > 0.0f)
		{
			temp = Mathf::ATan(diff[1]/diff[0]);
		}
		else if (diff[0] < 0.0f)
		{
			temp = Mathf::ATan(diff[1]/diff[0]) + Mathf::PI;
		}
		else
		{
			temp = Mathf::HALF_PI;
		}
		polars[i][0] = diff.Length();
		polars[i][1] = temp;
	}
}
//----------------------------------------------------------------------------
void MapTextureToQuad::SelectVertex (const Vector2f& position)
{
    // Identify vertex within 5 pixels of mouse click.
    const float pixelRange = 5.0f;
    mSelected = -1;
    for (int i = 0; i < 4; ++i)
    {
        Vector2f diff = position - mVertex[i];
        if (diff.Length() <= pixelRange)
        {
            mSelected = i;
            break;
        }
    }
}
Ejemplo n.º 3
0
//----------------------------------------------------------------------------
void MapTextureToQuad::SelectVertex (const Vector2f& rkPos)
{
    // identify vertex within 5 pixels of mouse click
    const float fPixels = 5.0f;
    m_iSelected = -1;
    for (int i = 0; i < 4; i++)
    {
        Vector2f kDiff = rkPos - m_akVertex[i];
        if ( kDiff.Length() <= fPixels )
        {
            m_iSelected = i;
            break;
        }
    }
}
Ejemplo n.º 4
0
float DistancetoLineSegment(const Vector2f& a, const Vector2f& b, const Vector2f& point)
{
	Vector2f dist = b - a;
	float length = dist.Length();
	float t = (point - a).Dot(dist);

	if(t < 0.0f)
		return (point - a).Length();
	if(t > length)
		return (point - b).Length();

	dist.Normalize();
	if(dist == Vector2f::Zero)
		return (point - a).Length();

	return (point - (a + dist * t)).Length();
}
Ejemplo n.º 5
0
//----------------------------------------------------------------------------
BspNode* BspNodes::CreateNode (const Vector2f& v0, const Vector2f& v1,
                               VertexColor3Effect* effect, const Float3& color)
{
	// Create the model-space separating plane.
	Vector2f dir = v1 - v0;
	AVector normal(dir[1], -dir[0], 0.0f);
	normal.Normalize();
	float constant = normal[0]*v0[0] + normal[1]*v0[1];
	HPlane modelPlane(normal, constant);

	// Create the BSP node.
	BspNode* bsp = new0 BspNode(modelPlane);

	VertexFormat* vformat = VertexFormat::Create(2,
	                        VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
	                        VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT3, 0);

	// Create the rectangle representation of the model plane and set the
	// vertex colors to the specified color.
	float xExtent = 0.5f*dir.Length();
	float yExtent = 0.125f;
	TriMesh* rect = StandardMesh(vformat).Rectangle(2, 2, xExtent, yExtent);
	VertexBufferAccessor vba(rect);
	for (int i = 0; i < 4; ++i)
	{
		vba.Color<Float3>(0, i) = color;
	}
	rect->SetEffectInstance(effect->CreateInstance());

	// Set the position and orientation for the world-space plane.
	APoint trn(0.5f*(v0[0] + v1[0]), 0.5f*(v0[1] + v1[1]), yExtent + 0.001f);
	HMatrix zRotate(AVector::UNIT_Z, Mathf::ATan2(dir.Y(),dir.X()));
	HMatrix xRotate(AVector::UNIT_X, Mathf::HALF_PI);
	HMatrix rotate = zRotate*xRotate;
	rect->LocalTransform.SetTranslate(trn);
	rect->LocalTransform.SetRotate(rotate);

	bsp->AttachCoplanarChild(rect);
	return bsp;
}