Represantación de puntos en el plano.

Este artículo continúa la serie dedicada a la representación geométrica en el plano usando JavaScript. Se puede consultar el artículo sobre la representación de las funciones trigonométricas. [ Consultar ] y el artículo sobre la representación de ejes cartesianos ortonormales [ Consultar ]

¿Qué valen unos ejes cartesianos? Por sí solos absolutamente nada. Pero son una herramienta genial para representar puntos. Como es sabido por la geometría analítica, los puntos están representados por dos coordenadas de tal modo que el par numérico (2,4) de abscisa 2 y ordenada 4. La función dibujaPunto(m,n) nos permitirá dibujar los puntos en JavaScript.

Indice.

1.- Ejemplos de puntos.

El contenedor de los ejes es 400x400 (pxs). El origen es el punto (200,200) y cada unidad es de 15 pxs.

Seleccione los ejemplos de la lista. Puede borrar los puntos en cualquier momento para repetir las pruebas.


2.- Código usado.

Esta podría ser una página que implementa la función dibujaPunto(m, n) y la ejes_cartesianos(origenX, origenY, pixelunid). El código *.css no es trivial y debería modificarse con sumo cuidado.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="generator" content="PSPad editor, www.pspad.com">
<title>
Puntos
</title>
<style type="text/css">
DIV.contenedorejes { background-color : black; width: 400; height : 400;clear : left; position : relative
; left : 0px; top : 0px; } DIV.ejeX{ width : 1px; height : 2px; clear : left; position : absolute; font-size : 0px; background-color : white; } DIV.ejeXcompleto{ clear : left; position : absolute; font-size : 0px; background-color : white; } DIV.ejeY{ width : 2px; height : 1px; clear : left; position : absolute; font-size : 0px; background-color : white; } DIV.ejeYcompleto{ clear : left; position : absolute; font-size : 0px; background-color : white; } DIV.punto{ width : 3px; height : 3px; clear : left; position : absolute; font-size : 0px; background-color : red; } DIV.texto{ clear : left; position : absolute; font-size : 11px; color :blue; }
</style>
<script type="text/javascript">
// ancho y alto del contenedor
var ancho = 400;
var alto = 400;
var x0 = 0;
var y0 = 0;
var unid_long
if(navigator.appVersion.indexOf("MSIE") != -1) var IE = true;
function ejes_cartesianos(origenX,origenY,pixelunid){
document.getElementById('contenedorejes').innerHTML = "";
// origenes de coordenadas
x0 = origenX;
y0 = origenY;
// longitud unidades
unid_long = pixelunid;
// numero de unidades eje x+
var num_unids_x_p = (ancho-x0)/unid_long;
// numero de unidades eje x-
var num_unids_x_n = x0/unid_long;
// numero unidades eje y+
var num_unids_y_p = y0/unid_long;
// numero unidades eje y-
var num_unids_y_n = (alto-y0)/unid_long;
// EJES
// EJE X completo
x = 0;
y = y0;
punto= document.createElement("div");
if(IE) punto.setAttribute("className","ejeXcompleto");
else punto.setAttribute("class","ejeXcompleto");
punto.style.left = x + "px";
punto.style.top = y + "px";
punto.style.width = ancho + "px";
punto.style.height = 1 + "px";
document.getElementById('contenedorejes').appendChild(punto);
// eje x divisiones
// eje x +
for(i=0;i<=num_unids_x_p;i++){
x = x0 + i*unid_long;
y = y0;
punto= document.createElement("div");
if(IE) punto.setAttribute("className","ejeX");
else punto.setAttribute("class","ejeX");
punto.style.left = x + "px";
punto.style.top = y + "px";
document.getElementById('contenedorejes').appendChild(punto);
}
// eje x-
for(i=0;i<=num_unids_x_n;i++){
x = x0 - i*unid_long;
y = y0;
punto= document.createElement("div");
if(IE) punto.setAttribute("className","ejeX");
else punto.setAttribute("class","ejeX");
punto.style.left = x + "px";
punto.style.top = y + "px";
document.getElementById('contenedorejes').appendChild(punto);
}
// EJE Y
// EJE Y completo
x = x0;
y = 0;
punto= document.createElement("div");
if(IE) punto.setAttribute("className","ejeXcompleto");
else punto.setAttribute("class","ejeXcompleto");
punto.style.left = x + "px";
punto.style.top = y + "px";
punto.style.width = 1 + "px";
punto.style.height = alto + "px";
document.getElementById('contenedorejes').appendChild(punto);
// eje y divisiones
// eje y+
for(i=0;i<=num_unids_y_p;i++){
x = x0;
y = y0 - i*unid_long;
punto= document.createElement("div");
if(IE) punto.setAttribute("className","ejeY");
else punto.setAttribute("class","ejeY");
punto.style.left = x + "px";
punto.style.top = y + "px";
document.getElementById('contenedorejes').appendChild(punto);
}
// eje y-
for(i=0;i<=num_unids_y_n;i++){
x = x0;
y = y0 + i*unid_long;
punto= document.createElement("div");
if(IE) punto.setAttribute("className","ejeY");
else punto.setAttribute("class","ejeY");
punto.style.left = x + "px";
punto.style.top = y + "px";
document.getElementById('contenedorejes').appendChild(punto);
}
}
function dibujaPunto(m,n){
var xP = m;
var yP = -n;
x = x0 + xP*unid_long;
y = y0 + yP*unid_long;
punto= document.createElement("div");
if(IE) punto.setAttribute("className","punto");
else punto.setAttribute("class","punto");
punto.style.left = x + "px";
punto.style.top = y + "px";
document.getElementById('contenedorejes').appendChild(punto);
// el texto que acompaña a los puntos
var texto = "("+ m + "," + n + ")";
textoPunto = document.createTextNode(texto);
divTexto = document.createElement("div");
if(IE) divTexto.setAttribute("className","texto");
else divTexto.setAttribute("class","texto");
divTexto.style.left = (x+6) + "px";
divTexto.style.top = (y-6) + "px";
divTexto.appendChild(textoPunto);
document.getElementById('contenedorejes').appendChild(divTexto);
}
</script>
</head>
<body onload="ejes_cartesianos(200,200,15);">
<table align="center">
<tr>
<td>
<table>

<tr>
<td>
<a href="javascript:dibujaPunto(-3,5)">Ejemplo 1</a>
<br>
<font size="-2">
Punto (-3,5)
</font>
</td>
</tr>
<tr>
<td>
<a href="javascript:dibujaPunto(5,1)">Ejemplo 2</a>
<br>
<font size="-2">
Punto (5,1)
</font>
</td>
</tr>
<tr>
<td>
<a href="javascript:dibujaPunto(5,-10)">Ejemplo 3</a>
<br>
<font size="-2">
Punto (5,-10)
</font>
</td>
</tr>
<tr>
<td>
<a href="javascript:dibujaPunto(-10,-6)">Ejemplo 4</a>
<br>
<font size="-2">
Punto (-10,-6)
</font>
</td>
</tr>
<tr>
<td>
<a href="javascript:dibujaPunto(0,-6)">Ejemplo 5</a>
<br>
<font size="-2">
Punto (0,-6)
</font>
</td>
</tr>
<tr>
<td>
<a href="javascript:dibujaPunto(10,0)">Ejemplo 6</a>
<br>
<font size="-2">
Punto (10,0)
</font>
</td>
</tr>
<tr>
<td>
<a href="javascript:ejes_cartesianos(200,200,15)">Borrar</a>
</td>
</tr>
</table>
</td>
<td>
<div id="contenedorejes" class="contenedorejes">
</div>
</td>
</tr>
</table>
</body>
</html>



Añadir a Favoritos Internet Explorer

Menú

Categorías


Lo Último

  Mis programas  Matemáticas  Geometría  Programación WEB  Flash MX  Variados
Dedicado a ANGELES y a RAMON.
Realizado por José Antonio López Lorenzo (JALL)
Vigo - Galicia - España -Europa