ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
distance.c
Go to the documentation of this file.
1/*****************************************************************************
2 * ugBASIC - an isomorphic BASIC language compiler for retrocomputers *
3 *****************************************************************************
4 * Copyright 2021-2026 Marco Spedaletti (asimov@mclink.it)
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *----------------------------------------------------------------------------
18 * Concesso in licenza secondo i termini della Licenza Apache, versione 2.0
19 * (la "Licenza"); è proibito usare questo file se non in conformità alla
20 * Licenza. Una copia della Licenza è disponibile all'indirizzo:
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Se non richiesto dalla legislazione vigente o concordato per iscritto,
25 * il software distribuito nei termini della Licenza è distribuito
26 * "COSÌ COM'È", SENZA GARANZIE O CONDIZIONI DI ALCUN TIPO, esplicite o
27 * implicite. Consultare la Licenza per il testo specifico che regola le
28 * autorizzazioni e le limitazioni previste dalla medesima.
29 ****************************************************************************/
30
31/****************************************************************************
32 * INCLUDE SECTION
33 ****************************************************************************/
34
35#include "../../ugbc.h"
36
37/****************************************************************************
38 * CODE SECTION
39 ****************************************************************************/
40
41extern char DATATYPE_AS_STRING[][16];
42
53/* <usermanual>
54@keyword DISTANCE
55
56@english
57
58The ''DISTANCE'' function calculates the geometric distance between two points.
59The distance is calculated with the application of the Pythagorean theorem,
60the result of which is however returned with the nnly integer part. If any
61component is omitted, the last screen position is used, instead.
62
63@italian
64La funzione ''DISTANCE'' calcola la distanza geometrica tra due punti.
65La distanza viene calcolata con l'applicazione del teorema di Pitagora, il cui
66risultato viene comunque restituito con la sola parte intera. Se viene omessa
67una qualsiasi delle componenti, viene usata quella corrispondente all'ultima
68position dello schermo.
69
70@syntax = DISTANCE( [x1], [y1] TO [x2], [y2] )
71
72@example result = DISTANCE( x1, y1 TO x2, y2 )
73
74@usedInExample maths_distance_01.bas
75</usermanual> */
76Variable * distance( Environment * _environment, char * _x1, char * _y1, char * _x2, char * _y2 ) {
77
78 Variable * x1 = variable_retrieve_or_define( _environment, _x1, VT_POSITION, 0 );
79 Variable * y1 = variable_retrieve_or_define( _environment, _y1, VT_POSITION, 0 );
80 Variable * x2 = variable_retrieve_or_define( _environment, _x2, VT_POSITION, 0 );
81 Variable * y2 = variable_retrieve_or_define( _environment, _y2, VT_POSITION, 0 );
82 Variable * two = variable_resident( _environment, VT_POSITION, "(two)");
83
84 variable_store( _environment, two->name, 2 );
85
86 Variable * sum = variable_add(
87 _environment,
88 powering( _environment,
89 variable_sub( _environment,
90 x1->name,
91 x2->name
92 )->name,
93 two->name
94 )->name,
95 powering( _environment,
96 variable_sub( _environment,
97 y1->name,
98 y2->name
99 )->name,
100 two->name
101 )->name
102 );
103
104 Variable * result = variable_cast( _environment, sum->name, VT_POSITION );
105
107
109 _environment,
110 sqroot(
111 _environment,
112 result->name
113 )->name,
114 result->name
115 );
116
117 return result;
118}
119
Variable * variable_add(Environment *_environment, char *_source, char *_destination)
Add two variable and return the sum of them.
Variable * variable_retrieve_or_define(Environment *_environment, char *_name, VariableType _type, int _value)
Variable * variable_move(Environment *_environment, char *_source, char *_destination)
Store the value of a variable inside another variable by converting it.
Variable * variable_resident(Environment *_environment, VariableType _type, char *_meaning)
Variable * variable_sub(Environment *_environment, char *_source, char *_dest)
Make a differenze between two variable and return the difference of them.
Variable * variable_cast(Environment *_environment, char *_source, VariableType _type)
Cast a variable from a type to another.
Variable * variable_store(Environment *_environment, char *_destination, unsigned int _value)
Store a direct value to a variable.
Variable * distance(Environment *_environment, char *_x1, char *_y1, char *_x2, char *_y2)
Return the distance between two (screen) positions.
Definition distance.c:76
Variable * powering(Environment *_environment, char *_base, char *_exponential)
Emit code to raise a variable to a given variable.
Definition pow.c:91
Variable * sqroot(Environment *_environment, char *_value)
Return the square root of a variable.
Definition sqr.c:98
char * name
Definition ugbc.h:979
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
@ VT_POSITION
Definition ugbc.h:468
#define MAKE_LABEL
Definition ugbc.h:3351
char DATATYPE_AS_STRING[][16]