0
我想創建一個程序,用戶可以在其中輸入座標點,並根據這些點生成一個三角形。不幸的是,當用戶輸入的第一面是斜邊。我的程序說這個三角形實際上不是一個直角三角形。SFML中的正三角形測試C++
#include<iostream>
#include<SFML/Graphics.hpp>
#include<math.h>
using namespace std;
using namespace sf;
int main(){
double x1;
double y1;
double x2;
double y2;
double x3;
double y3;
double DistanceP0;
double DistanceP1;
double DistanceP2;
int yes;
double area;
double area2;
double hyp;
double side1;
double side2;
cout << "Enter 6 integers. (it works better when those integers are greater than 100)" << endl;
cin >> x1;
cin >> y1;
cin >> x2;
cin >> y2;
cin >> x3;
cin >> y3;
RenderWindow window(VideoMode(1500,800), "SFML saves!");
window.setFramerateLimit(60);
sf:: ConvexShape Triangle;
Triangle.setPointCount(3);
Triangle.setPoint(0, sf::Vector2f(x1,y1));
Triangle.setPoint(1, sf::Vector2f(x2,y2));
Triangle.setPoint(2, sf::Vector2f(x3,y3));
DistanceP0 = sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));
DistanceP1 = sqrt(((x1 - y1) * (x1 - y1)) + ((x3 - y3) * (x3 - y3)));
DistanceP2 = sqrt(((x3 - x2) * (x3 - x2)) + ((y3 - y2) * (y3 - y2)));
//I am pretty sure that the error happens somewhere here{
if (DistanceP0 > DistanceP1 and DistanceP0 > DistanceP2)
{
hyp = DistanceP0;
if (DistanceP1 >= DistanceP2)
{
side1 = DistanceP1;
side2 = DistanceP2;
}
else(DistanceP1 <= DistanceP2){
side1 = DistanceP2;
side2 = DistanceP1;
}
}
else if (DistanceP1 > DistanceP0 and DistanceP1 > DistanceP2){
hyp = DistanceP1;
if (DistanceP0 >= DistanceP1)
{
side1 = DistanceP0;
side2 = DistanceP1;
}
else(DistanceP0 <= DistanceP1)
{
side1 = DistanceP1;
side2 = DistanceP0;
}
}
else {
hyp = DistanceP2;
if (DistanceP0 >= DistanceP1)
{
DistanceP0 = side1;
DistanceP1 = side2;
}
else(DistanceP0 <= DistanceP1)
{
DistanceP1 = side1;
DistanceP2 = side2;
}
}
if((side1 * side1) + (side2 * side2) == (hyp * hyp)){
yes = 1;
cout << "Your triangle is a right triangle" << endl;
}
else{
cout << "Your triangle is not a right triangle" << endl;
}
if(fabs(((side1 * side1) -(side2 * side2) - (hyp * hyp))) <= 0.00001){
yes = 1;
cout << "Your triangle is a right triangle" << endl;
}
//}
if(yes == 1){
cout << "The area of your triangle is " << (DistanceP0 * DistanceP1)/2 << endl;
area = (DistanceP0 * DistanceP1)/2;
}
area = sqrt(area);
RectangleShape RightSquare(Vector2f(area,area));
RightSquare.setPosition(x1,y1);
RightSquare.setFillColor(Color::Red);
if(yes!= 1){
double s = (DistanceP0 + DistanceP1 + DistanceP2)/2;
double R = s *((s - DistanceP0)*(s - DistanceP1) * (s - DistanceP2));
area2 = sqrt(fabs(R));
cout << "The area of your triangle is " << area2 << endl;
}
double area3 = sqrt(area2);
RectangleShape Square(Vector2f(area3,area3));
Square.setPosition(x1, y1);
Square.setFillColor(Color::Red);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event));
{
if(event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(Triangle);
if(yes == 1){
window.draw(RightSquare);
}
else{
window.draw(Square);
}
window.display();
}
return 0;
}
你可以給一些組座標用於此項目的工作? – user5954246
當然。 100,100,400,700,100,700。您輸入的所有其他座標都是工作的,但當第一條邊是斜邊時不會。 –