1votos

Dibujar Funcion cuadratica en Haskell

por josejuan hace 11 meses

Se abstrae cualquier función paramétrica (cualquier dominio y cualquier imagen) y se hace un renderizado a ASCII de cualquier paramétrica con parámetro unidimensional numérico (para dividir el dominio).

Dibujar un gráfico de una función cuadrática. - Esto no tiene que ver con el ejercicio, pero¿alguien me puede decir como usar esa pagina para poner avatars?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
{-# LANGUAGE MultiParamTypeClasses, TypeFamilies, FlexibleInstances #-} 
import Control.Arrow 
 
-- Si vamos a graficar algo, una generalización común es usar paramétricas 
-- aquí definimos una parametrización de cualquier cosa, desde cualquier 
-- dominio a cualquier imagen. 
-- 
class Parametric a v where 
  data ParametricData a v :: × 
  at   :: ParametricData a v → a → v 
  from :: ParametricData a v → a 
  to   :: ParametricData a v → a 
 
 
-- El anterior es muy genérico (ej. admite que el parámetro sea de varias 
-- dimensiones: 1 para líneas, 2 para superfícies, 3 para volúmenes, etc... 
-- Aunque podríamos definir también un parámetro n-dimensional, podemos 
-- restringir ya funciones a una única dimensión (para simplificar el 
-- muestreo en el dominio). Por ejemplo, para obtener n-muestras: 
 
genPoints :: (Fractional a, Enum a, Num a, Parametric a v, p ~ ParametricData a v) ⇒ p → Int → [v] 
genPoints p samples = at p ↥ [a, a + d … b] 
  where a = from p 
        b = to   p 
        d = (b - a) / fromIntegral samples 
 
 
 
-- Como ejemplo (único sobre el tipo instanciado), podemos definir la ecuación del enunciado 
-- o las que queramos 
instance Floating a ⇒ Parametric a (a, a) where 
  data ParametricData a (a, a) = Cuadrática { kA :: a, kB :: a, kC :: a, kF :: a, kT :: a } 
                               | Cúbica     { cA :: a, cB :: a, cC :: a, cD :: a, cF :: a, cT :: a } 
                               | Sombrero   { sA :: a, sB :: a, sF :: a, sT :: a } 
  at   (Cuadrática a b c _ _) t = (t, a × t² + b × t + c) 
  at   (Cúbica a b c d _ _)   t = (t, a × t³ + b × t² + c × t + d) 
  at   (Sombrero a b _ _)     t = (t, b × sin (a × t) / t) 
  from (Cuadrática _ _ _ f _)   = f 
  from (Cúbica _ _ _ _ f _)     = f 
  from (Sombrero _ _ f _)       = f 
  to   (Cuadrática _ _ _ _ t)   = t 
  to   (Cúbica _ _ _ _ _ t)     = t 
  to   (Sombrero _ _ _ t)       = t 
 
 
-- En la misma línea que la anterior, podemos renderizar a 2D (restringido por la instancia 
-- anterior) 
asciiRender :: (RealFrac a, Floating a, Enum a) ⇒ ParametricData a (a, a) → Int → [𝐒] 
asciiRender p samples = [[ if (x, y) ∈ vs then '*' else '·' | x ← [ax…bx]] | y ← [ay…by]] 
  where vs = (round ××× round) ↥ genPoints p samples 
        (ax, ay, bx, by) = (minimum $ fst ↥ vs, minimum $ snd ↥ vs, maximum $ fst ↥ vs, maximum $ snd ↥ vs) 
 
 
 
 
{- 
 
> mapM_ putStrLn $ asciiRender (Cuadrática 0.1 (-3) 0 (-1) 30) 200 
·············*******············ 
············**·····**··········· 
···········*·········*·········· 
··········*···········*········· 
·········**···········**········ 
········**·············**······· 
········*···············*······· 
·······**···············**······ 
·······*·················*······ 
······**··················*····· 
······*···················*····· 
·····*·····················*···· 
·····*·····················*···· 
····*······················**··· 
····*·······················*··· 
···**·······················**·· 
···*·························*·· 
···*·························*·· 
··*··························**· 
··*···························*· 
··*···························*· 
·*·····························* 
·*·····························* 
·*······························ 
*······························· 
*······························· 
 
 
> mapM_ putStrLn $ asciiRender (Cúbica (-1/4526) (1/252) (25/504) (3/28) (-30) 40) 200 
······································································* 
····································································*** 
···································································**·· 
·································································**···· 
······························································***······ 
···························································****········ 
··················*******************·················******··········· 
·············******·················*******************················ 
··········****························································· 
········***···························································· 
······**······························································· 
····**································································· 
··***·································································· 
·**···································································· 
**····································································· 
 
 
> mapM_ putStrLn $ asciiRender (Sombrero 0.2 (-70) (-60) 60) 200 
··························································*****·························································· 
························································***···***························································ 
·······················································**·······**······················································· 
······················································**·········**······················································ 
·····················································*·············*····················································· 
····················································*···············*···················································· 
···················································**···············**··················································· 
··················································*···················*·················································· 
··················································*···················*·················································· 
·················································*·····················*················································· 
················································*·······················*················································ 
···············································*·························*··············································· 
···················******·····················*···························*·····················******··················· 
···············****······***·················**···························**·················***······****··············· 
···········****·············**··············*·······························*··············**·············****··········· 
************··················***··········*·································*··········***··················************ 
································***······**···································**······***································ 
···································******·······································******··································· 
 
 
-} 

Comenta la solución

Tienes que identificarte para poder publicar tu comentario.