-
Data: 2011-06-18 20:30:12
Temat: Re: jak szacowac dokladnosc obliczen
Od: Radoslaw Jocz <r...@p...onet.pl> szukaj wiadomości tego autora
[ pokaż wszystkie nagłówki ]
spojrzcie na ten kod nie rozumiem zbytno po co ktos
robil takie sztuczki z przesunieciami bitowymi << 14 >> 14
typedef struct point {
int x;
int y;
} point;
typedef struct line {
point p1;
point p2;
} line;
/*
* check_lines:
* This is based off an explanation and expanded math presented by Paul
Bourke:
*
* It takes two lines as inputs and returns 1 if they intersect, 0 if
they do
* not. hitp returns the point where the two lines intersected.
*
* This function expects integer value inputs and stores an integer value
* in hitp if the two lines interesect. The internal calculations are
fixed
* point with a 14 bit fractional precision for processors without floating
* point units.
*/
int check_lines(line *line1, line *line2, point *hitp)
{
/* Introduction:
* This code is based on the solution of these two input equations:
* Pa = P1 + ua (P2-P1)
* Pb = P3 + ub (P4-P3)
*
* Where line one is composed of points P1 and P2 and line two is
composed
* of points P3 and P4.
*
* ua/b is the fractional value you can multiple the x and y legs
of the
* triangle formed by each line to find a point on the line.
*
* The two equations can be expanded to their x/y components:
* Pa.x = p1.x + ua(p2.x - p1.x)
* Pa.y = p1.y + ua(p2.y - p1.y)
*
* Pb.x = p3.x + ub(p4.x - p3.x)
* Pb.y = p3.y + ub(p4.y - p3.y)
*
* When Pa.x == Pb.x and Pa.y == Pb.y the lines intersect so you
can come
* up with two equations (one for x and one for y):
*
* p1.x + ua(p2.x - p1.x) = p3.x + ub(p4.x - p3.x)
* p1.y + ua(p2.y - p1.y) = p3.y + ub(p4.y - p3.y)
*
* ua and ub can then be individually solved for. This results in the
* equations used in the following code.
*/
/* Denominator for ua and ub are the same so store this calculation */
int d = (line2->p2.y - line2->p1.y)*(line1->p2.x-line1->p1.x) -
(line2->p2.x - line2->p1.x)*(line1->p2.y-line1->p1.y);
/* n_a and n_b are calculated as seperate values for readability */
int n_a = (line2->p2.x - line2->p1.x)*(line1->p1.y-line2->p1.y) -
(line2->p2.y - line2->p1.y)*(line1->p1.x-line2->p1.x);
int n_b = (line1->p2.x - line1->p1.x)*(line1->p1.y - line2->p1.y) -
(line1->p2.y - line1->p1.y)*(line1->p1.x - line2->p1.x);
/* Make sure there is not a division by zero - this also indicates that
* the lines are parallel.
*
* If n_a and n_b were both equal to zero the lines would be on top
of each
* other (coincidental). This check is not done because it is not
* necessary for this implementation (the parallel check accounts
for this).
*/
if(d == 0)
return 0;
/* Calculate the intermediate fractional point that the lines
potentially
* intersect.
*/
int ua = (n_a << 14)/d;
int ub = (n_b << 14)/d;
/* The fractional point will be between 0 and 1 inclusive if the lines
* intersect. If the fractional calculation is larger than 1 or
smaller
* than 0 the lines would need to be longer to intersect.
*/
if(ua >=0 && ua <= (1<<14) && ub >= 0 && ub <= (1<<14))
{
hitp->x = line1->p1.x + ((ua * (line1->p2.x - line1->p1.x))>>14);
hitp->y = line1->p1.y + ((ua * (line1->p2.y - line1->p1.y))>>14);
return 1;
}
return 0;
}
Następne wpisy z tego wątku
- 18.06.11 20:33 Radoslaw Jocz
- 18.06.11 20:36 Jacek Czerwinski
- 18.06.11 20:40 slawek
- 18.06.11 20:47 Jacek Czerwinski
- 18.06.11 21:20 qwak
- 18.06.11 22:49 Michoo
- 19.06.11 08:17 Radoslaw Jocz
- 19.06.11 08:49 Radoslaw Jocz
- 19.06.11 10:59 slawek
- 19.06.11 21:30 Artur Muszyński
- 20.06.11 08:03 Paweł Kierski
- 27.06.11 06:53 Tomasz Kaczanowski
Najnowsze wątki z tej grupy
- Xiaomi [Chiny - przyp. JMJ] produkuje w całkowitych ciemnościach i bez ludzi
- Prezydent SZAP/USONA Trump ułaskawił prezydenta Hondurasu Hernandeza skazanego na 45 lat więzienia
- Rosjanie chwalą się prototypem komputera kwantowego. "Najważniejszy projekt naukowy Rosji"
- A Szwajcarzy kombinują tak: FinalSpark grows human neurons from stem cells and connects them to electrode arrays
- Re: Najgorszy język programowania
- NOWY: 2025-09-29 Alg., Strukt. Danych i Tech. Prog. - komentarz.pdf
- Na grupie comp.os.linux.advocacy CrudeSausage twierdzi, że Micro$lop używa SI do szyfrowania formatu dok. XML
- Błąd w Sofcie Powodem Wymiany 3 Duńskich Fregat Typu Iver Huitfeldt
- Grok zaczął nadużywać wulgaryzmów i wprost obrażać niektóre znane osoby
- Can you activate BMW 48V 10Ah Li-Ion battery, connecting to CAN-USB laptop interface ?
- We Wrocławiu ruszyła Odra 5, pierwszy w Polsce komputer kwantowy z nadprzewodzącymi kubitami
- Ada-Europe - AEiC 2025 early registration deadline imminent
- John Carmack twierdzi, że gdyby gry były optymalizowane, to wystarczyły by stare kompy
- Ada-Europe Int.Conf. Reliable Software Technologies, AEiC 2025
- Linuks od wer. 6.15 przestanie wspierać procesory 486 i będzie wymagać min. Pentium
Najnowsze wątki
- 2026-01-29 KSeF - 13 wątpliwości
- 2026-01-29 A ja się pochwalę
- 2026-01-29 Warszawa => Mid/Senior IT Recruiter <=
- 2026-01-29 Warszawa => Senior Java Developer <=
- 2026-01-29 Warszawa => IT Recruiter <=
- 2026-01-28 Degradacja
- 2026-01-28 Wysoki Sąd poinstruował czego unikać wyzywając Owsiaka "Równiejszego"
- 2026-01-28 Białystok => Solution Architect (Workday) - Legal Systems <=
- 2026-01-28 Białystok => Preseles Inżynier (background baz danych) <=
- 2026-01-28 Wrocław => Konsultant wdrożeniowy ERP <=
- 2026-01-28 Łódź => Microsoft Engineer <=
- 2026-01-28 Białystok => Tester manualny <=
- 2026-01-27 Tradycja ciągania posłów po sądach za wystąpienia w Sejmie będzie kontynuowana [Lepper 2]
- 2026-01-27 Pierwszy raz sprzedano więcej samochodów zeeletryfikowanych niż ice
- 2026-01-27 Elektryczny Kałasznikow




Co zmienia darmowy dostęp do Rejestru Cen Nieruchomości?