Diferencia entre revisiones de «Usuario:ManuelRomero/ProgramacionWeb/php/POO/Herencia/EjemploGeometria/Triangulo»
De WikiEducator
(Página creada con «<font size<nowiki>=</nowiki>5 color<nowiki>=</nowiki>red> Volver temario </font> <hr />») |
|||
Línea 3: | Línea 3: | ||
</font> | </font> | ||
<hr /> | <hr /> | ||
+ | ;Clase Triangulo | ||
+ | <source lang=php> | ||
+ | <?php | ||
+ | |||
+ | //put your code here | ||
+ | class Triangulo extends Poligono{ | ||
+ | function __construct($base,$altura){ | ||
+ | parent::__construct(3,$base,$altura); | ||
+ | } | ||
+ | |||
+ | public function area() { | ||
+ | return $this->base * $this->altura /2; | ||
+ | } | ||
+ | |||
+ | public function dibuja() { | ||
+ | $texto = "Triángulo de base ".$this->area()." px"; | ||
+ | $codigoHtml = <<<FIN | ||
+ | <canvas height="500px" width="1024px" id="canvas"> | ||
+ | Su navegador no soporta en elemento CANVAS</canvas> | ||
+ | <script lang=javascript> | ||
+ | var canvas = document.getElementById('canvas'); | ||
+ | var ctx = canvas.getContext('2d'); | ||
+ | ctx.lineWidth=3; | ||
+ | ctx.beginPath(); | ||
+ | var x=25; | ||
+ | var y=500; | ||
+ | |||
+ | //Dibujo la base | ||
+ | ctx.moveTo(x,y); | ||
+ | ctx.lineTo(x+$this->base,y);//Dibjujo la base | ||
+ | ctx.strokeStyle = "#f00"; | ||
+ | ctx.stroke(); | ||
+ | |||
+ | //Dibujo un lado lateral | ||
+ | ctx.lineTo(x+($this->base/2),y-$this->altura);//Dibjujo la base | ||
+ | |||
+ | ctx.strokeStyle = "#f00"; | ||
+ | ctx.stroke(); | ||
+ | //Dibujo otro lado lateral | ||
+ | ctx.lineTo(x,y); | ||
+ | ctx.strokeStyle = "#f00"; | ||
+ | ctx.stroke(); | ||
+ | |||
+ | ctx.font = '24px "Tahoma"'; | ||
+ | |||
+ | ctx.fillText("$texto", 25,500-($this->altura+50)); | ||
+ | ctx.font = '10px "Tahoma"'; | ||
+ | var txt = "$texto"; | ||
+ | ctx.fillText("2", (25+(txt.length)*10.5),500-($this->altura+60)); | ||
+ | |||
+ | </script> | ||
+ | FIN; | ||
+ | return $codigoHtml; | ||
+ | } | ||
+ | } | ||
+ | </source > |
Última revisión de 13:57 7 dic 2016
- Clase Triangulo
<?php //put your code here class Triangulo extends Poligono{ function __construct($base,$altura){ parent::__construct(3,$base,$altura); } public function area() { return $this->base * $this->altura /2; } public function dibuja() { $texto = "Triángulo de base ".$this->area()." px"; $codigoHtml = <<<FIN <canvas height="500px" width="1024px" id="canvas"> Su navegador no soporta en elemento CANVAS</canvas> <script lang=javascript> var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); ctx.lineWidth=3; ctx.beginPath(); var x=25; var y=500; //Dibujo la base ctx.moveTo(x,y); ctx.lineTo(x+$this->base,y);//Dibjujo la base ctx.strokeStyle = "#f00"; ctx.stroke(); //Dibujo un lado lateral ctx.lineTo(x+($this->base/2),y-$this->altura);//Dibjujo la base ctx.strokeStyle = "#f00"; ctx.stroke(); //Dibujo otro lado lateral ctx.lineTo(x,y); ctx.strokeStyle = "#f00"; ctx.stroke(); ctx.font = '24px "Tahoma"'; ctx.fillText("$texto", 25,500-($this->altura+50)); ctx.font = '10px "Tahoma"'; var txt = "$texto"; ctx.fillText("2", (25+(txt.length)*10.5),500-($this->altura+60)); </script> FIN; return $codigoHtml; } }