A
A
Artem Lebedev2020-04-13 15:12:48
C++ / C#
Artem Lebedev, 2020-04-13 15:12:48

How to change the program?

Good day, there is the following problem: a triangle is set on the plane with the coordinates of the vertices (x1, y1), (x2, y2), (x3, y3) and an arbitrary point (x, y). Write a boolean function to check if a point lies inside a triangle.
I myself thought of a simple solution using several similar codes, but it turned out without a function.
Here is the code without the boolean function:

// тп5.cpp: определяет точку входа для консольного приложения.
//
#include "stdafx.h"
#include <stdio.h>
#include <locale>
 
int main()
{
    setlocale(LC_ALL, "rus");
    double x1, y1, x2, y2, x3, y3, x, y;
    printf("Введите координаты вершины A => ");
    scanf("%lf%lf", &x1, &y1); // читаем координаты точки A
    printf("Введите координаты вершины В => ");
    scanf("%lf%lf", &x2, &y2); // читаем координаты точки B
    printf("Введите координаты вершины С => ");
    scanf("%lf%lf", &x3, &y3); // читаем координаты точки C
    printf("Введите координаты проверяемой точки => ");
    scanf("%lf%lf", &x, &y); // читаем координаты точки D
    printf(
        (((x - x1)*(y2 - y1) - (y - y1)*(x2 - x1))*((x3 - x1)*(y2 - y1) - (y3 - y1)*(x2 - x1)) >= 0) &&
        (((x - x2)*(y3 - y2) - (y - y2)*(x3 - x2))*((x1 - x2)*(y3 - y2) - (y1 - y2)*(x3 - x2)) >= 0) &&
        (((x - x3)*(y1 - y3) - (y - y3)*(x1 - x3))*((x2 - x3)*(y1 - y3) - (y2 - y3)*(x1 - x3)) >= 0) ?
        "Точка входит в треугольник \n" : "Точка не входит в треугольник \n");
    return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur Pokrovsky, 2020-04-13
@lrik64

The question is not very clear.
Did you mean it?

bool func (double x1, double y1, double x2, double y2, double x3, double y3, double x, double y) {
    return (((x - x1)*(y2 - y1) - (y - y1)*(x2 - x1))*((x3 - x1)*(y2 - y1) - (y3 - y1)*(x2 - x1)) >= 0) &&
           (((x - x2)*(y3 - y2) - (y - y2)*(x3 - x2))*((x1 - x2)*(y3 - y2) - (y1 - y2)*(x3 - x2)) >= 0) &&
           (((x - x3)*(y1 - y3) - (y - y3)*(x1 - x3))*((x2 - x3)*(y1 - y3) - (y2 - y3)*(x1 - x3)) >= 0)
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question