64,648
社区成员
发帖
与我相关
我的任务
分享
#ifndef _TRIANGLE_H_INCLUDED
#define _TRIANGLE_H_INCLUDED
#include <cmath>
template< typename T >
struct Point
{
T x_;
T y_;
Point<T>& operator = ( const Point<T>& other )
{
x_ = other.x_;
y_ = other.y_;
return *this;
}
};
template< typename T >
T distance( const Point<T>& p1, const Point<T>& p2 )
{
long double dx = p1.x_ - p2.x_;
long double dy = p1.y_ - p2.y_;
long double ans = sqrt( dx*dx + dy*dy );
return static_cast<T>(ans);
}
template< typename T >
struct Triang
{
Point<T> a_;
Point<T> b_;
Point<T> c_;
};
template< typename T >
T area( const Triang<T>& tr )
{
const T a = distance( a_, b_ );
const T b = distance( b_, c_ );
const T c = distance( c_, a_ );
long double s = static_cast<long double>( a + b + c ) / 2;
long double ans = s * ( s-a ) * ( s-b ) * ( s-c );
return static_cast<T>( sqrt(ans) );
}
template< typename T >
bool contain_orig( const Triang<T>& tr )
{
const T area = area( tr );
Triangle<T> small_triang;
Point<T> orig;
orig.x_ = T();
orig.y_ = T();
small_triang.a_ = tr.a_;
small_triang.b_ = tr.b_;
small_triang.c_ = orig;
const T area1 = area( small_triang );
small_triang.b_ = tr.c_;
const T area2 = area( small_triang );
small_triang.a = tr.b_;
const T area3 = area( small_triang );
return ( area == area1 + area2 + area3 );
}
template<>
bool contain_orig( const Triang<float>& tr )
{
float eps = 1e-6;
const float area = area( floatr );
Triangle<float> small_triang;
Poinfloat<float> orig;
orig.x_ = float();
orig.y_ = float();
small_triang.a_ = floatr.a_;
small_triang.b_ = floatr.b_;
small_triang.c_ = orig;
const float area1 = area( small_triang );
small_triang.b_ = floatr.c_;
const float area2 = area( small_triang );
small_triang.a = floatr.b_;
const float area3 = area( small_triang );
return ( area - ( area1 + area2 + area3 ) < eps );
}
#endif
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
void main()
{
double x = 0;
double y = 0;
double xnum[3];
double ynum[3];
cout<<"请分别输入三点的坐标:"<<endl;
int i=0;
while(i!=3)
{
cin>>x>>y;
xnum[i] = x;
ynum[i] = y;
i++;
}
double rim1 = sqrt(((0-xnum[0])*(0-xnum[0]))+((0-ynum[0])*(0-ynum[0])));
double rim2 = sqrt(((0-xnum[1])*(0-xnum[1]))+((0-ynum[1])*(0-ynum[1])));
double rim3 = sqrt(((0-xnum[2])*(0-xnum[2]))+((0-ynum[2])*(0-ynum[2])));
double l12 = sqrt(((xnum[0]-xnum[1])*(xnum[0]-xnum[1]))+((ynum[0]-ynum[1])*(ynum[0]-ynum[1])));
double l13 = sqrt(((xnum[0]-xnum[2])*(xnum[0]-xnum[2]))+((ynum[0]-ynum[2])*(ynum[0]-ynum[2])));
double l23 = sqrt(((xnum[1]-xnum[2])*(xnum[1]-xnum[2]))+((ynum[1]-ynum[2])*(ynum[1]-ynum[2])));
if(rim1<rim2&&rim1<rim3)
{
if((rim1+rim2+rim3)<=(l12+l13))
cout<<"原点在三角形内";
else
cout<<"原点不在三角形内";
}
if(rim2<rim1&&rim2<rim3)
{
if((rim1+rim2+rim3)<=(l12+l23))
cout<<"原点在三角形内";
else
cout<<"原点不在三角形内";
}
if(rim3<rim2&&rim3<rim1)
{
if((rim1+rim2+rim3)<=(l23+l13))
cout<<"原点在三角形内";
else
cout<<"原点不在三角形内";
}
}
#include <stdio.h>
typedef struct _Point
{
double x;
double y;
}Point;
int main()
{
Point a = {1,0},b = {-1,0},c = {0,1};
double x,y,z;
if((x = a.x*b.y - a.y*b.x) < 0)
printf("It is not contain the zero point!");
else if((y = b.x*c.y - b.y*c.x) < 0)
printf("It is not contain the zero point!");
else if((z = c.x*a.y - c.y*a.x) < 0)
printf("It is not contain the zero point!");
else
printf("It is contain the zero point!");
return 0;
}