graph2D.component.ts 63.5 KB
Newer Older
1
import { Component, ViewChild, ElementRef } from '@angular/core';
Diego Rey's avatar
Diego Rey committed
2
3
import { GHCIService } from '../../../shared/services/ghci.service';
import functionPlot from 'function-plot';
4
import { Animation, Setting, toJSON, triggerDownload } from './graph2D.helper';
5
import { TranslateService } from '@ngx-translate/core';
Diego Rey's avatar
Diego Rey committed
6

7
8
@Component({
    moduleId: module.id,
Diego Rey's avatar
Diego Rey committed
9
10
    selector: 'graph2D-component',
    templateUrl: './graph2D.component.html',
11
    styleUrls: ['./graph2D.component.scss'],
12
    host: {
13
        '(window:resize)': 'onResize($event)'
14
15
    }
})
Diego Rey's avatar
Diego Rey committed
16
export class Graph2DComponent {
Diego Rey's avatar
Diego Rey committed
17

18
    // Ghci Service
19
20
    private ghciServiceSub: any;

21
22
23
24
25
26
27
28
29
30
31
32
33
    // Chart Container - DOM Element
    @ViewChild('graph2DContainer')
    private graph2DRef: ElementRef;

    // Chart Instance
    private instance: any;

    // Settings
    settings: Setting = {
        axis: true,
        grid: true,
        tip: true
    }
34

35
36
37
38
    private funciones= [];

    private id = 0;

39
40
    private valores = [];

41
42
    private conjunto= [];

43
    // Animation state
44
    animation: Animation = {
45
        data: [],
46
        init: false,
47
        currentFrame: 0,
48
        fps: 10,
49
        playing: false,
50
51
52
        timeout: null,
        animationFrame: null,
        speedX: 10,
53
54
        boton: true,
        zoo: 2000
55
    };
56
57
    //Nuevo

58
59
60
61
62
63
64
    public easeInOutCubic = function(fps) {
        var t = fps < 6 ? 6 : fps;
        var k = t/60;
        var animation = k<.5 ? 60*(4*k*k*k) : 60*((k-1)*(2*k-2)*(2*k-2)+1);
        console.log(animation);
        return animation;
    }
Diego Rey's avatar
Diego Rey committed
65

66
    public setZoom = () => {
67
        this.animation.zoo = this.animation.zoo ;  
68
69
70
71
    }
    public multiGraf = () => { 
        this.animation.boton = !this.animation.boton;
    }
72
    public constructor(private ghciService: GHCIService, public translate: TranslateService) {
73
        this.ghciServiceSub = ghciService.messages.subscribe(
Diego Rey's avatar
Diego Rey committed
74
            canvas => {
75
76
77
78
                // Stop Animation
                if (this.animation.init) {
                    this.stopAnimation();
                }
79
80
                switch(canvas.tipo) { 
                    case 'graph': {
leonelrosano's avatar
leonelrosano committed
81
                        console.log(canvas.resultado)
leonelrosano's avatar
leonelrosano committed
82
83
84
                        // var jsonCanvas = JSON.parse(JSONRepair(canvas.resultado));

                       var jsonCanvas = JSON.parse(canvas.resultado);
85
86
                        var conjs = this.obtenerConjunto(jsonCanvas.funs[0]);
    
87
                        var d = conjs + "}"; //Leo
88
                        var obj = JSON.parse(d);
89

90
91
92
                        //Para las funciones 
                        if (obj.conj.sets.fdom == "function(x)") {
                            var nom = jsonCanvas.funs[0].dom;
93
                            var elemento1 = this.recursionfuncion(jsonCanvas.funs[0].sets, nom);
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
                            

                            var funcionString = '';
                            funcionString ="var "+nom+" = function(x){\nreturn "+elemento1+"}\n"

                            funcionString += 'return ' + nom + '(x);\n'
                            
                            for (var funs of jsonCanvas.funs) {
                                funcionString = 'var ' + funs.fun + ' = function('+funs.args+'){\n return ' + this.generarExpresion(funs.bdy) + '}\n' + funcionString;
                            }
                            funcionString = '(x)=>{\n' + funcionString + '}';
                            obj.conj.sets.fdom = eval(funcionString);








114
                        }
115
116
                        if (obj.conj.sets.fcod == "function(x)") {
                            var nom = jsonCanvas.funs[0].cod;
117
118
                            var elemento2 = this.recursionfuncion(jsonCanvas.funs[0].sets, nom);
                            obj.conj.sets.fcod = function (x) { return (eval(elemento2)) }
119
120
121
122
123
124
125
126
127
128
129
130
131
132

                            var funcionString = '';
                            funcionString ="var "+nom+" = function(x){\nreturn "+elemento2+"}\n"

                            funcionString += 'return ' + nom + '(x);\n'
                            
                            for (var funs of jsonCanvas.funs) {
                                funcionString = 'var ' + funs.fun + ' = function('+funs.args+'){\n return ' + this.generarExpresion(funs.bdy) + '}\n' + funcionString;
                            }
                            funcionString = '(x)=>{\n' + funcionString + '}';

                            obj.conj.sets.fdom = eval(funcionString);


133
                        }
134
135

                        var funcionGenerada = this.generarFuncion(jsonCanvas);
136
137
138
                    
 

139

140
141
142
                        //para Enumerados
                        if (obj.conj.dom == 'Numer') {
                            var j = 0;
143
144
145
146
                            for (var f of obj.conj.sets.fdom) {
                                var expresionDom = new RegExp('( '+f+' )', 'g'); 
                                funcionGenerada = funcionGenerada.replace(expresionDom, j.toString()); 
                                j += 1;
147
148
149
                            }
                        }
                        if (obj.conj.cod == 'Numer') {
150
151
152
153
154
                            var j2 = 0;
                            for (var f2 of obj.conj.sets.fcod) {
                                var expresionCod = new RegExp(f2, 'g'); 
                                funcionGenerada = funcionGenerada.replace(expresionCod, j2.toString()); 
                                j2 += 1;
155
                            }
156

157
                        }
158

leonelrosano's avatar
leonelrosano committed
159
160
161
162
163
164
165
166
167
168
                        if (obj.conj.baseDom == 'R'){
                            obj.conj.baseCod = 'R';
                            if (obj.conj.cod != 'Func'){
                                obj.conj.cod = 'R';
                                obj.conj.fcod = 'R'; 
                            }
             

                        }

169
170
                        
                        
171
172
                        let fun = eval(funcionGenerada);

leonelrosano's avatar
leonelrosano committed
173
174
                        var colores = ['violet', 'red', 'blue', 'orange', 'green','black']
                        var num = this.getRandomArbitrary(0, 5);
175
176
177
178
179
180
181
182
                        var color = colores[num];
    
                        var tipoGraf;
                        if (obj.conj.baseDom != 'R'){
                            tipoGraf = 'scatter';
                        }else{
                            tipoGraf = 'polyline';
                        }
leonelrosano's avatar
leonelrosano committed
183
                        console.log(obj.conj);
184
                        if(this.animation.boton && obj.conj.cod != 'Numer' && obj.conj.dom != 'Numer'){
185
186
187
188
189
                            if(this.conjunto.length == 1 && (this.conjunto[0].cod == 'Numer' || this.conjunto[0].dom == 'Numer')){
                                this.conjunto = [];
                                this.conjunto.push(obj.conj);
                                this.id = 0;
                                this.funciones = [];
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
                                if(obj.conj.baseDom == 'R'){
                                    this.valores = [];
                                    var funcionGenerada2 = this.generarFuncionDisc(jsonCanvas);
                                    
                                    var funcionesVer2019 =  this.createListFunction(jsonCanvas);
                                    var listValores = [];
                                    for (var k of this.valores){
                                    listValores.push(parseInt(k))
                                    }   
                                    for (var i of this.valores){
                                    for(var t of this.valores){
                                        var aux = parseInt(t);
                                        var aux2 = parseInt(i);
                                        var mul = aux * aux2;
                                        listValores.push(mul)
                                        }
                                    }
                                    for (i = -999; i < 1000; i++) {                                      
                                        listValores.push(i)
                                    }
                                    let sinRepetidos = listValores.filter((valor, indiceActual, arreglo) => arreglo.indexOf(valor) === indiceActual);

                                    for (var fun2 of funcionesVer2019){
                                        let fun3 = eval(fun2);
                                        var insertar = false;
                                        var punto1 = [];

                                        if (sinRepetidos.length){
                                            for (var p of sinRepetidos){
                                                var pn= parseInt(p) - 0.001
                                                var pp= parseInt(p) + 0.001
                                                insertar = (((fun3(pp) != undefined) ) || ((fun3(pn) != undefined)) || insertar)
                                                if((fun3(pp) != undefined)){
                                                    punto1.push(pp);
                                                }else if ((fun3(pn) != undefined)){
                                                    punto1.push(p);
                                                }
                                            }
                                        }else{
                                            insertar = true;
                                        }
                                        if (insertar){
                                            this.funciones.push({
                                                id: 0,
                                                sampler: 'builtIn',
                                                fn: function(scope) {
                                                return fun3(scope.x)
                                                },
                                                graphType: tipoGraf,
                                                point:punto1[0],
                                                color: color,
                                                
                                            });
                                        }
                                    } 
                                }else{
       
                                        this.funciones.push({
leonelrosano's avatar
leonelrosano committed
248

249
250
251
252
253
254
255
256
257
258
                                            id: this.funciones.length,
                                            sampler: 'builtIn',
                                            fn: function(scope) {
                                            return fun(scope.x)
                                            },
                                            graphType: tipoGraf,
                                            color: color
                                        })
                                } 
                                     
259
260
261
                            }else{
                                if(this.conjunto.length == 1){
                                    this.id = 1;
262
                              
263
264
265
                                    this.conjunto.unshift({radio: 2, dom:this.conjunto[0].baseDom, cod:this.conjunto[0].baseCod, baseCod:this.conjunto[0].baseCod, baseDom:this.conjunto[0].baseDom, sets:{fdom:this.conjunto[0].baseDom,fcod:this.conjunto[0].baseCod}});
                                    this.funciones[0].id = this.id;
                                    
266
                                }
267
268
269
270
271
272
273
274
275
276
277
278
279
                                if (this.conjunto.length != 0){ 
                                    if (obj.conj.baseDom == 'R'){
                                        this.conjunto[0].baseDom = 'R';
                                        this.conjunto[0].dom = 'R';
                                        this.conjunto[0].sets.fdom = 'R';
                                    }
                                    if (obj.conj.baseCod == 'R'){
                                        this.conjunto[0].baseCod = 'R';
                                        this.conjunto[0].cod = 'R';
                                        this.conjunto[0].sets.fcod = 'R';
                                    }
                                }    
                                this.conjunto.push(obj.conj);
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
                                var identificador=0;
                                    if (this.conjunto.length > 1){
                                        identificador=this.conjunto.length - 2 + this.id;
                                    }
                                if(obj.conj.baseDom == 'R'){
                                   this.valores = [];
                                   var funcionGenerada2 = this.generarFuncionDisc(jsonCanvas);

                                   var funcionesVer2019 =  this.createListFunction(jsonCanvas);
                                   var listValores = [];
                                   for (var k of this.valores){
                                      listValores.push(parseInt(k))
                                   }   
                                   for (var i of this.valores){
                                      for(var t of this.valores){
                                         var aux = parseInt(t);
                                         var aux2 = parseInt(i);
                                         var mul = aux * aux2;
                                         listValores.push(mul)
                                       }
                                    }
                                    for (i = -999; i < 1000; i++) {                                      
                                        listValores.push(i)
                                    }
                                    let sinRepetidos = listValores.filter((valor, indiceActual, arreglo) => arreglo.indexOf(valor) === indiceActual);

                                    for (var fun2 of funcionesVer2019){
                                        let fun3 = eval(fun2);
                                        var insertar = false;
                                        var punto1 = [];

                                        if (sinRepetidos.length){
                                            for (var p of sinRepetidos){
                                                var pn= parseInt(p) - 0.001
                                                var pp= parseInt(p) + 0.001
                                                insertar = (((fun3(pp) != undefined) ) || ((fun3(pn) != undefined)) || insertar)
                                                if((fun3(pp) != undefined)){
                                                    punto1.push(pp);
                                                }else if ((fun3(pn) != undefined)){
                                                    punto1.push(p);
                                                }
                                            }
                                        }else{
                                            insertar = true;
                                        }
                                        if (insertar){
                                            this.funciones.push({
                                                id: identificador,
                                                sampler: 'builtIn',
                                                fn: function(scope) {
                                                return fun3(scope.x)
                                                },
                                                graphType: tipoGraf,
                                                point:punto1[0],
                                                color: color,
                                               
                                            });
                                        }
                                    } 
                                }else{
                                    this.funciones.push({
                                        id: identificador,
                                        sampler: 'builtIn',
                                        fn: function(scope) {
                                        return fun(scope.x)
                                        },
                                        graphType: tipoGraf,
                                        color: color
                                    });
                                }
                                
                            }        
                        }else{ 
                            
                            this.conjunto = [];
                            this.conjunto.push(obj.conj);
                            this.id = 0;
                            this.funciones = [];
                            if(obj.conj.baseDom == 'R'){
                                this.valores = [];
                                var funcionGenerada2 = this.generarFuncionDisc(jsonCanvas);
                                var funcionesVer2019 =  this.createListFunction(jsonCanvas);
                                var listValores = [];
                                for (var k of this.valores){
                                   listValores.push(parseInt(k))
                                }
                                for (var i of this.valores){
                                   for(var t of this.valores){
                                      var aux = parseInt(t);
                                      var aux2 = parseInt(i);
                                      var mul = aux * aux2;
                                      listValores.push(mul)
                                    }
                                 }
                                 for (i = -999; i < 1000; i++) {                                      
                                     listValores.push(i)
                                 }
                                 let sinRepetidos = listValores.filter((valor, indiceActual, arreglo) => arreglo.indexOf(valor) === indiceActual);

                                 for (var fun2 of funcionesVer2019){
                                     let fun3 = eval(fun2);
                                     var insertar = false;
                                     var punto1 = [];

                                     if (sinRepetidos.length){
                                         for (var p of sinRepetidos){
                                             var pn= parseInt(p) - 0.001
                                             var pp= parseInt(p) + 0.001
                                             insertar = (((fun3(pp) != undefined) ) || ((fun3(pn) != undefined)) || insertar)
                                             if((fun3(pp) != undefined)){
                                                 punto1.push(pp);
                                             }else if ((fun3(pn) != undefined)){
                                                 punto1.push(p);
                                             }
                                         }
                                     }else{
                                         insertar = true;
                                     }
                                     if (insertar){
                                         this.funciones.push({
                                             id: 0,
                                             sampler: 'builtIn',
                                             fn: function(scope) {
                                             return fun3(scope.x)
                                             },
                                             graphType: tipoGraf,
                                             point:punto1[0],
                                             color: color,
                                            
                                         });

                                     }
                                  
                                 } 
414

415
                            }else{
416
                                this.funciones.push({
417
                                    id: this.funciones.length,
418
419
420
421
422
423
424
                                    sampler: 'builtIn',
                                    fn: function(scope) {
                                    return fun(scope.x)
                                    },
                                    graphType: tipoGraf,
                                    color: color
                                });
425
426

                            }
427
                        }
428
                        let bounding = this.getBounding();
429
430
                        this.instance = functionPlot({
                            target: '#graph2D-container',
431
432
                            width: bounding.width,
                            height: bounding.height,
Diego Rey's avatar
Diego Rey committed
433
434
435
436
                            tip: { 
                                color: 'green'
                            },
                            xAxis: {
437
438
439
440
441
442
443
444
                                scale: 'linear',
                                domain: { initial: [-4, 4],
                                        type: 'discrete' },
                                yAxis: { domain: [-4, 4] }
                            },
                            conj:this.conjunto,
                            data: this.funciones,
                            zoom:this.animation.zoo,
445
446
447
448
449
                            plugins: [
                                functionPlot.plugins.zoomBox()
                            ]
                        })
                        break; 
450
                    }
451
                    case 'canvas': {
452
                        var shapesData = JSON.parse(canvas.resultado);
453
                        var shapesDataNormalized = this.normalizeShapesData(shapesData);
454
455
                        let bounding = this.getBounding();
                        this.cleanPlot();
456
457
                        this.instance = functionPlot({
                            target: '#graph2D-container',
458
459
460
                            width: bounding.width,
                            height: bounding.height,
                            grid: true,
461
462
463
464
465
466
467
468
469
470
471
472
473
                            xAxis: {
                                scale: 'linear',
                                domain: {
                                    initial: [-10, 10],
                                    type: 'discrete'
                                }
                            },
                            data: shapesDataNormalized,
                            plugins: [
                                functionPlot.plugins.zoomBox()
                            ]
                        })
                        break; 
474
                    }
475
476
                    case 'animacion': {
                        this.cleanPlot();                  
477
478
479
                        var animationData = canvas.resultado.map(res => JSON.parse(res));
                        for (var frame of animationData) {
                            this.animation.data.push(this.normalizeShapesData(frame));
480
                        }
481
482
                        this.runAnimation();
                        this.animation.init = true;
483
484
                        break; 
                    }
485
                }
Diego Rey's avatar
Diego Rey committed
486
487
            },
            error => {
488
489
490
            }
        )
    }
Diego Rey's avatar
Diego Rey committed
491

492
493
494
495
496
497
    /**
     * Angular lifecycle hook.
     * called after Angular has fully initialized a component's view.
     */
    ngAfterViewInit() {
        if (!this.instance) {
498
            let bounding = this.getBounding();
499
500
            this.instance = functionPlot({
                target: '#graph2D-container',
501
502
503
                width: bounding.width,
                height: bounding.height,
                grid: true,
504
505
506
507
508
509
510
                xAxis: {
                    scale: 'linear',
                    domain: {
                        initial: [-10, 10],
                        type: 'discrete'
                    }
                },
511
                data: []
512
513
514
515
516
517
518
519
520
521
522
523
524
525
            })
        }
    }

    /**
     * Angular lifecycle hook.
     * called when a directive, pipe, or service is destroyed.
     */
    ngOnDestroy() {
        if (this.ghciServiceSub) {
          this.ghciServiceSub.unsubscribe();
        }
    }

526
527
528
529
530
531
    /**
     * On Resize Event.
     */
    onResize(event){
        let instance = this.instance;
        let bounding = this.getBounding();
532
        if (bounding.width > 0) {
533
534
535
            instance.options.width = bounding.width;
            instance.options.height = bounding.height;
            instance.build();
536
        }
537
538
539
540
541
542
543
    }

    /**
     * @name getBounding
     * @desc get the measures of the container of the graph
     */
    private getBounding = function() {
Diego Rey's avatar
Diego Rey committed
544
545
546
547
548
        var {width, height} = this.graph2DRef.nativeElement.getBoundingClientRect();
        if (width === 0) {
            width = document.getElementById('graph2D-container').querySelector('svg').width.baseVal.value;
            height = document.getElementById('graph2D-container').querySelector('svg').height.baseVal.value;
        }
549
550
551
        return {width, height}
    }

552
553
554
555
    /**
     * @name updateFrame
     * @desc update data for Function Plot and redraw the graph
     */
556
557
558
559
560
561
    public updateFrame = function() {
        var $this = this;
        var $data = $this.animation.data[$this.animation.currentFrame];
        if ($this.instance) {
            $this.instance.options.data = $data;
            $this.instance.draw();
562
        } else {
563
564
            let bounding = $this.getBounding();
            $this.instance = functionPlot({
565
                target: '#graph2D-container',
566
567
                width: bounding.width,
                height: bounding.height,
568
569
570
571
572
573
574
575
                xAxis: {
                    label: 'x - axis',
                    scale: 'linear',
                    domain: {
                        initial: [-10, 10],
                        type: 'discrete'
                    }
                },
576
                data: $data
Diego Rey's avatar
Diego Rey committed
577
            })
578
        }
579
        // Update Frame
580
581
582
583
        $this.animation.timeout = setTimeout(function() {
            $this.animation.currentFrame = ($this.animation.currentFrame + 1) % $this.animation.data.length;
            $this.animation.animationFrame = requestAnimationFrame($this.updateFrame.bind($this));
        }, 1000/ this.easeInOutCubic($this.animation.fps));
584
    }
585

586
587
588
589
590
    /**
     * @name runAnimation
     * @desc Run Shapes Animation
     */
    public runAnimation = function() {
591
        if (this.animation.playing) return;
592
593
        var $this = this;
        $this.animation.playing = true;
594
        $this.updateFrame();
595
    }
596

597
598
599
600
601
602
    /**
     * @name pauseAnimation
     * @desc Pause Shapes Animation
     */
    public pauseAnimation = function() {
        var $this = this;
603
604
605
        cancelAnimationFrame($this.animation.animationFrame);
        clearTimeout($this.animation.timeout);
        $this.animation.timeout = null;
606
        $this.animation.playing = false;
Diego Rey's avatar
Diego Rey committed
607
    }
608

609
610
611
612
613
614
    /**
     * @name stopAnimation
     * @desc Stop Shapes Animation
     */
    public stopAnimation = function() {
        var $this = this;
615
        $this.pauseAnimation();
616
617
        $this.animation.data = [];
        $this.animation.currentFrame = 0;
Diego Rey's avatar
Diego Rey committed
618
        $this.animation.init = false;
619
620
        this.instance.removeAllGraphs();
    }
Diego Rey's avatar
Diego Rey committed
621

Diego Rey's avatar
Diego Rey committed
622
623
624
625
626
    /**
     * @name decreaseSpeed
     * @desc Decrease Speed Animation
     */
    public decreaseSpeed = function() {
627
628
629
630
631
632
633
634
635
636
637
638
        var decrease = false;
        if (this.animation.fps > 6) {
            decrease = true;
        }
        if (decrease) {
            if (this.animation.fps > 10) {
                this.animation.fps -= 1;
                this.animation.speedX = this.animation.fps / 10;
            } else {
                this.animation.fps -= 1;
                this.animation.speedX = this.animation.fps / 10;
            }
Diego Rey's avatar
Diego Rey committed
639
640
641
642
643
644
645
646
647
648
            this.pauseAnimation();
            this.runAnimation();
        }
    }
    
    /**
     * @name restoreSpeed
     * @desc Increase Speed Animation
     */
    public restoreSpeed = function() {
649
        this.animation.fps = 10;
Diego Rey's avatar
Diego Rey committed
650
651
652
653
654
        this.animation.speedX = 1;
        this.pauseAnimation();
        this.runAnimation();
    }

655
    /**
656
657
658
659
     * @name increaseSpeed
     * @desc Increase Speed Animation
     */
    public increaseSpeed = function() {
660
661
662
663
664
665
666
667
668
669
670
671
        var increase = false;
        if (this.animation.fps < 60) {
            increase = true;
        }
        if (increase) {
            if (this.animation.fps >= 10) {
                this.animation.fps += 1;
                this.animation.speedX = this.animation.fps / 10;
            } else {
                this.animation.fps += 1;
                this.animation.speedX = this.animation.fps / 10;
            }
Diego Rey's avatar
Diego Rey committed
672
673
674
            this.pauseAnimation();
            this.runAnimation();
        }
675
676
677
678
679
680
681
682
    }


    /**
     * @name toggleGrid
     * @desc Show and Hide Grid
     */
    public toggleGrid = function () {
683
        this.instance.toggleGrid();
Diego Rey's avatar
Diego Rey committed
684
        this.settings.grid = !this.settings.grid;
685
686
687
688
689
690
691
    }

    /**
     * @name toggleAxis
     * @desc Show and Hide Axis
     */
    public toggleAxis = function () {
692
        this.instance.toggleAxis();
Diego Rey's avatar
Diego Rey committed
693
        this.settings.axis = !this.settings.axis;
694
695
696
697
698
699
700
    }

    /**
     * @name toggleTip
     * @desc Show and Hide Tip
     */
    public toggleTip = function () {
701
        this.instance.toggleTip();
Diego Rey's avatar
Diego Rey committed
702
        this.settings.tip = !this.settings.tip;
703
704
705
    }

    /**
706
707
708
709
     * @name zoomOut
     * @desc Zoom Out Button Control
     */
    public zoomOut = function () {
Diego Rey's avatar
Diego Rey committed
710
        this.instance.zoomOut();
711
    }
712
713
714
715
716
    /**
     * @name zoomIn
     * @desc Zoom In Button Control 
     */
    public zoomIn = function () {
Diego Rey's avatar
Diego Rey committed
717
718
        this.instance.zoomIn();
    }
719
720
721
722
723
724
725
726
727
728
729
730
    /**
     * @name recenterPlot
     * @desc center the plot and it returns to the initial state.
     */
    public recenterPlot = function () {
        this.instance.recenter();
    }
    /**
     * @name cleanPlot
     * @desc remove all the graph from the instance.
     */
    public cleanPlot = function () {
731
732
733
        if (this.animation.playing) {
            this.stopAnimation();
        } else {
734
735
736
            this.funciones = [];
            this.conjunto = [];
            this.id = 0;
737
738
            this.instance.removeAllGraphs();
        }
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
    }

    /**
     * @name exportPlot
     * @desc Download Plot as an SVG image.
     */
    public exportPlot = function() {
        // Objects
        var svg = document.querySelector('svg');
        var canvas = document.createElement("canvas");

        // Set dimensions of the image
        var svgSize = svg.getBoundingClientRect();
        canvas.width = svgSize.width;
        canvas.height = svgSize.height;

        // Convert SVG DOM structure to xml
        var ctx = canvas.getContext('2d');
        var data = new XMLSerializer().serializeToString(svg);

        // URL Object used to parse, construct, normalise, and encode URLs.
        var DOMURL = window.URL || (<any>window).webkitURL || window;

        var img = new Image();
        var svgBlob = new Blob([data], { type: 'image/svg+xml;charset=utf-8' });
        var url = DOMURL.createObjectURL(svgBlob);

        img.onload = function () {
            ctx.drawImage(img, 0, 0);
            DOMURL.revokeObjectURL(url);

            var imgURI = canvas
                .toDataURL('image/png')
                .replace('image/png', 'image/octet-stream');

            triggerDownload(imgURI);
        };
        img.src = url;
    }

779

780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
    /**
     * @name normalizeRectData
     * @desc Normalize Rectangle data for Function Plot Library 
     * @param {Object} rectData Data of Rectangle to be normalized
     * @returns {Object}
     */
    public normalizeRectData = function ($rectData) {
        var $rectNormalized:any = {};
        var $shape:any = {};
        $shape.w = $rectData.w;
        $shape.h = $rectData.h;
        $shape.x = $rectData.x;
        $shape.y = $rectData.y;
        $rectData.color && ($shape.fill = $rectData.color);
        $rectData.rot !== 'undefined' && ($shape.rotation = $rectData.rot);

        $rectNormalized.shape = $shape;
        $rectNormalized.graphType = 'shape';
        $rectNormalized.shapeType = 'rect';
    
        return $rectNormalized;
    }
    /**
     * @name normalizeCircleData
     * @desc Normalize Circle data for Function Plot Library 
     * @param {Object} circleData Data of Circle to be normalized
     * @returns {Object}
     */
    public normalizeCircleData = function ($circleData) {
        var $circleNormalized:any = {};
        var $shape:any = {};
        $shape.r = $circleData.r;
        $shape.x = $circleData.x;
        $shape.y = $circleData.y;
        $circleData.color && ($shape.fill = $circleData.color);
        $circleData.rot !== 'undefined' && ($shape.rotation = $circleData.rot);

        $circleNormalized.shape = $shape;
        $circleNormalized.graphType = 'shape';
        $circleNormalized.shapeType = 'circle';
    
        return $circleNormalized;
    }
823
    /**
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
     * @name normalizeTextData
     * @desc Normalize Text data for Function Plot Library 
     * @param {Object} textData Data of Text to be normalized
     * @returns {Object}
     */
    public normalizeTextData = function ($textData) {
        var $textNormalized:any = {};
        var $shape:any = {};
        $shape.text = $textData.text;
        $shape.size = $textData.size;
        $shape.x = $textData.x;
        $shape.y = $textData.y;
        $textData.color && ($shape.fill = $textData.color);
        $textData.rot !== 'undefined' && ($shape.rotation = $textData.rot);

        $textNormalized.shape = $shape;
        $textNormalized.graphType = 'shape';
        $textNormalized.shapeType = 'text';
    
        return $textNormalized;
    }
845
846
847
848
    /**
     * @name normalizeLineData
     * @desc Normalize Line data for Function Plot Library 
     * @param {Object} lineData Data of Line to be normalized
849
850
     * @returns {Object}
     */
Diego Rey's avatar
Diego Rey committed
851
852
853
854
855
856
857
858
859
860
861
862
    public normalizeLineData = function ($lineData) {
        var $lineNormalized:any = {};
        var $points = []
        for (var p of $lineData.pts) {
            $points.push([p[0],p[1]]);
        }
        $lineNormalized.points = $points;
        $lineNormalized.stroke = $lineData.color;
        $lineNormalized.rotation = $lineData.rot;
        $lineNormalized.fnType = 'points';
        $lineNormalized.polylineType = 'line';
        $lineNormalized.graphType = 'polyline';
863
    
Diego Rey's avatar
Diego Rey committed
864
865
        return $lineNormalized;
    }
866
867
868
869
    /**
     * @name normalizePolygonData
     * @desc Normalize Polygon data for Function Plot Library 
     * @param {Object} textData Data of Polygon to be normalized
870
871
     * @returns {Object}
     */
Diego Rey's avatar
Diego Rey committed
872
873
874
875
876
877
878
879
880
881
882
883
    public normalizePolygonData = function ($polygonData) {
        var $polygonNormalized:any = {};
        var $points = []
        for (var p of $polygonData.pts) {
            $points.push([p[0],p[1]]);
        }
        $polygonNormalized.points = $points;
        $polygonNormalized.fill = $polygonData.color;
        $polygonNormalized.rotation = $polygonData.rot;
        $polygonNormalized.fnType = 'points';
        $polygonNormalized.polylineType = 'polygon';
        $polygonNormalized.graphType = 'polyline';
884
    
Diego Rey's avatar
Diego Rey committed
885
886
        return $polygonNormalized;
    }
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908

    /**
     * @name normalizeShapesData
     * @desc Normalize Shapes data for Function Plot Library 
     * @param {Array} shapesData Data of Shapes to be normalized 
     * @returns {Array}
     */
    public normalizeShapesData = function (shapesData) {
        var normalized:Array<Object> = [];
        for (var shape of shapesData) {
            switch(shape.kind) { 
                case 'rect': { 
                    normalized.push(this.normalizeRectData(shape)); 
                    break; 
                } 
                case 'circle': { 
                    normalized.push(this.normalizeCircleData(shape)); 
                    break; 
                } 
                case 'text': { 
                    normalized.push(this.normalizeTextData(shape)); 
                    break; 
909
                }
Diego Rey's avatar
Diego Rey committed
910
911
912
913
914
915
916
917
                case 'line': { 
                    normalized.push(this.normalizeLineData(shape)); 
                    break; 
                }
                case 'poly': { 
                    normalized.push(this.normalizePolygonData(shape)); 
                    break; 
                } 
918
919
920
921
922
            } 
        }
        return normalized;
    }

923
924


Diego Rey's avatar
Diego Rey committed
925
    getRandomArbitrary = function (min, max) {
926
927
        return Math.round(Math.random() * (max - min) + min);
    }
Diego Rey's avatar
Diego Rey committed
928
929
930
931
932
933
934
935
936
937

    generarFuncion = function (graph) {
        var funcionString = '';
        var grafica;
        for (var fun of graph.funs) {
            funcionString = 'var ' + fun.fun + ' = function(' + fun.args.join() + '){\n return ' + this.generarExpresion(fun.bdy) + '}\n' + funcionString;

            if (fun.fun == graph.graph) {
                funcionString += 'return ' + fun.fun + '(' + fun.args.join() + ');\n'
                grafica = fun;
938
939
            }
        }
940
        funcionString = '(' + grafica.args.join() + ')=>{\n' + funcionString + '}';
Diego Rey's avatar
Diego Rey committed
941
942
943

        return funcionString;
    }
944

Diego Rey's avatar
Diego Rey committed
945
    generarExpresion = function (exp) {
946
947
        var expresion = '';
        if (exp.kind == 'cnd') {
Diego Rey's avatar
Diego Rey committed
948
            expresion = ' (' + this.generarExpresion(exp.cond) + '?' + this.generarExpresion(exp.exp1) + ':' + this.generarExpresion(exp.exp2) + ') ';
949
        } else if (exp.kind == 'bop') {
Diego Rey's avatar
Diego Rey committed
950
            if (exp.op == '==') {
951
                expresion = ' Math.abs((' + this.generarExpresion(exp.exp1) + ') - (' + this.generarExpresion(exp.exp2) + ')) == 0 ';
Diego Rey's avatar
Diego Rey committed
952
            } else if (exp.op == '/=') {
953
                expresion = ' Math.abs((' + this.generarExpresion(exp.exp1) + ') - (' + this.generarExpresion(exp.exp2) + ')) != 0';
Diego Rey's avatar
Diego Rey committed
954
955
956
957
958
            } else if (exp.op == '^') {
                expresion = ' Math.pow(' + this.generarExpresion(exp.exp1) + ',' + this.generarExpresion(exp.exp2) + ') ';
            } else {
                expresion = ' (' + this.generarExpresion(exp.exp1) + ')' + exp.op + '(' + this.generarExpresion(exp.exp2) + ') ';
            }
959
        } else if (exp.kind == 'uop') {
Diego Rey's avatar
Diego Rey committed
960
            expresion = ' ' + exp.op + ' ' + this.generarExpresion(exp.exp) + ' ';
961
        } else if (exp.kind == 'app') {
962
963
964
965
966
967
968
			
			if (exp.fun == 'cos') {
				exp.fun = 'Math.cos'
			} else if (exp.fun == 'sin') {
				exp.fun = 'Math.sin'
			} else if (exp.fun == 'round') {
				exp.fun = 'Math.round'
leonelrosano's avatar
leonelrosano committed
969
970
971
			}else if (exp.fun == 'sqrt'){
                exp.fun = 'Math.sqrt'
            }
972
973
			expresion = ' ' + exp.fun + '(' + exp.args.map(e => this.generarExpresion(e)).join() + ') ';

974
        } else if (exp.kind == 'tup') {
Diego Rey's avatar
Diego Rey committed
975
            expresion = ' (' + exp.exps.map(e => this.generarExpresion(e)).join() + ') ';
976
        } else if (exp.kind == 'lit') {
Diego Rey's avatar
Diego Rey committed
977
            expresion = ' ' + exp.val + ' ';
978
        } else if (exp.kind == 'var') {
Diego Rey's avatar
Diego Rey committed
979
            expresion = ' ' + exp.var + ' ';
980
        } else {
Diego Rey's avatar
Diego Rey committed
981
            expresion = ' undefined ';
982
        }
Diego Rey's avatar
Diego Rey committed
983

984
985
        return expresion;
    }
986
987
988



989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
// nueva
createListFunction = function (graph) {
    var funcionString = '';
    var grafica;
    var funciones = [];
    var j = 0;
    while (graph.funs[j].fun != graph.graph) {
        j += 1;
    }

    var fun1 = graph.funs[j];
    var arrayFunction = [];
    var nameFun = [];
    nameFun.push(fun1.fun);
    funciones = this.armarFuncion(fun1.bdy,graph,nameFun);

    for (var funs of funciones){
        for (var fun of graph.funs){
            if (fun.fun != fun1.fun){
                funcionString = 'var ' + fun.fun + ' = function(' + fun.args.join() + '){\n return ' + this.generarExpresion(fun.bdy) + '}\n' + funcionString;
            }
        }
        funcionString = 'var ' + fun1.fun + ' = function(' + fun1.args.join() + '){\n return ' + funs + '}\n' + funcionString;

        funcionString += ';return ' + fun1.fun + '(' + fun1.args.join() + ');\n'
        grafica = fun1;
        funcionString = '(' + grafica.args.join() + ')=>{\n' + funcionString + '}';
        arrayFunction.push(funcionString);   
        var funcionString = '';
    }
    return arrayFunction;
}

armarFuncion = function (exp,graph,nameFun){
    var lisArm = this.generateFunctionAndExp(exp,graph,nameFun);
    var lisFun = [];
    for(var lis of lisArm){
        var aux = '';
        if(lis[0] == 'N'){
            aux = lis[1];
        }else{
            aux = '('+lis[0]+' ? '+lis[1]+' : undefined )';
        }
        lisFun.push(aux);
    }
    return lisFun
}


generateFunctionAndExp = function (exp,graph,namefun6) {
    var myList = [];
    // devuelvo lista con tupla (cond,funcion)
    if (exp.kind == 'cnd') {
        var lisA = this.generateFunctionAndExp(exp.exp1,graph,namefun6);
        var lisB = this.generateFunctionAndExp(exp.exp2,graph,namefun6);
        var cond = this.createListExp(exp.cond);
        var g = this.createListExp(exp.cond);
        var aux121 = [];
        var cond2 = g.splice(1);
        var union = g[0]


        for(var a of cond2){
            union = '('+union+' && '+a+')';
        }
        for (var fun1 of lisA){
            if(fun1[0] == 'N'){
                if(cond.length == 0){
                    aux121[0]='N';
                }else{
                    aux121[0]=union;
                }
            }else{ 
                aux121[0]= '('+fun1[0]+' && '+union+ ')';
            }
            aux121[1] = fun1[1];
            myList.push(aux121);
        }
        for (var fun2 of lisB){
            if(cond.length != 0){
                for (var condi of cond){
                    var aux123 = [];
                    aux123[1]=fun2[1];
                    if(fun2[0] == 'N'){
                            aux123[0]='!('+condi+')';
                    }else{
                        aux123[0] = '('+fun2[0]+' && !('+condi+'))';
                    }
                    myList.push(aux123);
                }
            }else{
                myList.push(fun2);
            }
        }
        
    } else if (exp.kind == 'bop') {
        
        if (exp.op == '^') {
            var lisA1 = this.generateFunctionAndExp(exp.exp1,graph,namefun6);
            var lisB1 = this.generateFunctionAndExp(exp.exp2,graph,namefun6);
            var aux1 = [];
            for (var f1 of lisA1){
                for (var f2 of lisB1){
                    var aux11 = 'Math.pow('+f1[1]+' , '+f2[1]+')';
                    var aux21;
                    if ((f2[0] == 'N') && (f1[0] == 'N')){
                        aux21 = 'N'
                    }else if(f2[0] == 'N'){
                        aux21=f1[0]
                    }else if(f1[0] == 'N'){
                        aux21=f2[0]
                    }else{
                        aux21= '('+f1[0]+' && '+f2[0]+')';
                    }
                    aux1[0]= aux21;
                    aux1[1] = aux11;
                    myList.push(aux1);
                }
            }
        } else {

            var lisA2 = this.generateFunctionAndExp(exp.exp1,graph,namefun6);
            var lisB2 = this.generateFunctionAndExp(exp.exp2,graph,namefun6);
            var aux2 = [];
            for (var f1 of lisA2){
                for (var f2 of lisB2){
                    var aux12 = '(('+f1[1]+')'+exp.op+'('+f2[1]+'))';
                    var aux22
                    if ((f2[0] == 'N') && (f1[0] == 'N')){
                        aux22 = 'N'
                    }else if (f2[0] == 'N'){
                        aux22 =f1[0]
                    }else if (f1[0] == 'N'){
                        aux22 =f2[0]
                    }else{ 
                        aux22= '('+f1[0]+' && '+f2[0]+')';
                    }
                    aux2[0]= aux22;
                    aux2[1] = aux12;
                    myList.push(aux2);

                }
            }
        }
    } else if (exp.kind == 'uop') {
        var lisA3 = this.generateFunctionAndExp(exp.exp,graph,namefun6);
        var aux3 = [];
        for (var f1 of lisA3){
                var aux13 = ' '+exp.op+' '+f1[1]+' ';               
                aux3[0]= f1[0];
                aux3[1] = aux13;
                myList.push(aux3);   
        }
    } else if (exp.kind == 'app') {       
        if ((exp.fun == 'Math.cos') || (exp.fun == 'cos')) {
            var aux4 = [];
            var aux14 = 'Math.cos('+ exp.args.map(e => this.generarExpresion(e)).join() +')';
            var aux24 = 'N'
            aux4[0]= aux24;
            aux4[1] = aux14;
            myList.push(aux4);           
        } else if ((exp.fun == 'Math.sin') || (exp.fun == 'sin')) {
            var aux5 = [];
            var aux15 = 'Math.sin('+ exp.args.map(e => this.generarExpresion(e)).join() +')';
            var aux25 = 'N'
            aux5[0]= aux25;
            aux5[1] = aux15;
            myList.push(aux5);
        } else if ((exp.fun == 'Math.round') || (exp.fun == 'round')){
            var aux6 = [];
            var aux16 = 'Math.round('+ exp.args.map(e => this.generarExpresion(e)).join() +')';
            var aux26 = 'N'
            aux6[0]= aux26;
            aux6[1] = aux16;
            myList.push(aux6);
leonelrosano's avatar
leonelrosano committed
1164
1165
1166
1167
1168
1169
1170
        }else if ((exp.fun == 'Math.sqrt') || (exp.fun == 'sqrt')){
            var aux7 = [];
            var aux17 = 'Math.sqrt('+ exp.args.map(e => this.generarExpresion(e)).join() +')';
            var aux27 = 'N'
            aux7[0]= aux27;
            aux7[1] = aux17;
            myList.push(aux7);
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
        }else{
            if(!namefun6.includes(exp.fun)){
                var ListnameFNew = namefun6;
                ListnameFNew.push(exp.fun);
                for (var fun5 of graph.funs){
                    if(fun5.fun == exp.fun){
                         myList = this.generateFunctionAndExp(fun5.bdy,graph,ListnameFNew);
                    }
                }
            }else{
                var aux76 = [];
                aux76[0] = 'N';
                aux76[1] = exp.fun;
                myList.push(aux76);
            }
        }
        
    } else if (exp.kind == 'tup') {
        var lisA7 = this.generateFunctionAndExp(exp.exps,graph,namefun6);
        for (var f of lisA7){
            var aux65 = [];
            aux65[1] = '('+f[1]+')';
            aux65[0] = f[0];
            myList.push(aux65);
        }
       //Es esto o las combinaciones
       // expresion = ' (' + exp.exps.map(e => this.generarExpresion(e)).join() + ') ';
    } else if (exp.kind == 'lit') {
        var aux8 = [];
        aux8[0] = 'N';
        aux8[1] = ' ' + exp.val + ' ';
        myList.push(aux8);
    } else if (exp.kind == 'var') {

        var aux9 = [];
        aux9[0] = 'N';
        aux9[1] = ' ' + exp.var + ' ';
        myList.push(aux9);
    } else {
        var aux54 = [];
        aux54[0]= 'N';
        aux54[1]='undefined';
        myList.push(aux54);
    }

    return myList;
}

/////////////////////

createListExp = function (exp) {
    var expresion = '';
    var funcione = [];
    if (exp.kind == 'cnd') {
            funcione = this.createListExp(exp.cond);
    } else if (exp.kind == 'bop') {
        if (exp.op == '==') {
            expresion = ' Math.abs((' + this.createListExp(exp.exp1)[0] + ') - (' + this.createListExp(exp.exp2)[0] + ')) == 0 ';
            if(exp.exp1.kind == 'lit'){
                this.valores.push(exp.exp1.val)
            }
            if(exp.exp2.kind == 'lit'){
                this.valores.push(exp.exp2.val)
            }
            funcione.push(expresion)

        } else if (exp.op == '/=') {
            expresion = ' Math.abs((' + this.createListExp(exp.exp1) + ') - (' + this.createListExp(exp.exp2) + ')) != 0';
            funcione.push(expresion)

        } else if (exp.op == '^') {
            expresion = ' Math.pow(' + this.createListExp(exp.exp1) + ',' + this.createListExp(exp.exp2) + ') ';
            funcione.push(expresion)

        } else if (exp.op == '&&'){
            
            for (var atr of this.createListExp(exp.exp1)){
                funcione.push(atr)
            }
            for (var atr2 of this.createListExp(exp.exp2)){
                funcione.push(atr2)
            }
        }else if (exp.op == 'or'){
            funcione.push('!'+this.createListExp(exp.exp1))
            funcione.push('!'+this.createListExp(exp.exp2))
        }else{
            var lis1 = this.createListExp(exp.exp1)
            var lis2 = this.createListExp(exp.exp2)
            for(var expes1 of lis1){
                for (var expes2 of lis2){
                    expresion = ' ((' + expes1 + ')' + exp.op + '(' + expes2 + ') )';
                    funcione.push(expresion)
                }
            }
        }
    } else if (exp.kind == 'uop') {
        expresion = ' ' + exp.op + ' ' + this.createListExp(exp.exp) + ' ';
        funcione.push(expresion)

    } else if (exp.kind == 'app') {

        if (exp.fun == 'Math.cos') {
            exp.fun = 'Math.cos'
            expresion = ' ' + exp.fun + '(' + exp.args.map(e => this.generarExpresion(e)).join() + ') ';
            funcione.push(expresion)

        } else if (exp.fun == 'Math.sin') {
            exp.fun = 'Math.sin'
            expresion = ' ' + exp.fun + '(' + exp.args.map(e => this.generarExpresion(e)).join() + ') ';
            funcione.push(expresion)

        } else if (exp.fun == 'Math.round') {
            exp.fun = 'Math.round'
            expresion = ' ' + exp.fun + '(' + exp.args.map(e => this.generarExpresion(e)).join() + ') ';
            funcione.push(expresion)

leonelrosano's avatar
leonelrosano committed
1287
1288
1289
1290
1291
        } else if (exp.fun == 'Math.sqrt') {
            exp.fun = 'Math.sqrt'
            expresion = ' ' + exp.fun + '(' + exp.args.map(e => this.generarExpresion(e)).join() + ') ';
            funcione.push(expresion)

1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
        }else{
            expresion = ' ' + exp.fun + '(' + exp.args.map(e => this.generarExpresion(e)).join() + ') ';
            funcione.push(expresion)
        }

    } else if (exp.kind == 'tup') {
        expresion = ' (' + exp.exps.map(e => this.generarExpresion(e)).join() + ') ';
        funcione.push(expresion)

    } else if (exp.kind == 'lit') {
        expresion = ' ' + exp.val + ' ';
        this.valores.push(exp.val)
        funcione.push(expresion)

    } else if (exp.kind == 'var') {

        expresion = ' ' + exp.var + ' ';
        funcione.push(expresion)

    } else {
        expresion = 'undefined ';
        funcione.push(expresion)

    }
    return funcione;
}
///////////////////// fin de nuevo


     // Prueba 2019
     generarFuncionDisc = function (graph) {
        var funcionString = '';
        var grafica;
        var arrayFunction = [];
        var fun = graph.funs[0];
        var ListnameF = [];
        ListnameF.push(fun.fun);
        var funciones = this.generarExpresionDisc(fun.bdy,graph,ListnameF)
        for (var funs of funciones){
            funcionString = 'var ' + fun.fun + ' = function(' + fun.args.join() + '){\n return ' + funs + '}\n' + funcionString;

            if (fun.fun == graph.graph) {
                funcionString += ';return ' + fun.fun + '(' + fun.args.join() + ');\n'
                grafica = fun;
            }
            funcionString = '(' + grafica.args.join() + ')=>{\n' + funcionString + '}';
            arrayFunction.push(funcionString);
            var funcionString = '';
        }


        var arrayFunction1 = [arrayFunction[0]]
        return arrayFunction;
    }

    generarExpresionDisc = function (exp,grap,ListnameF) {
        var expresion = '';
        var funcione = [];
        if (exp.kind == 'cnd') {

                var con1 = this.generarExpresionDisc(exp.cond,grap,ListnameF);
                var con11 = '';
                
                for (var condi of con1){
                    if (con11 == ''){
                        con11 = condi;
                    }else{
                        con11 = '('+con11 +' && '+ condi +')';
                    }
                    
                }
                for (var funs of this.generarExpresionDisc(exp.exp1,grap,ListnameF)){ 
                    funcione.push('('+con11+' ? '+ funs +' : undefined)');      
                }
                for (var funs2 of this.generarExpresionDisc(exp.exp2,grap,ListnameF)){ 
                    for (var condi2 of con1){
                        funcione.push('(!('+condi2+') ? '+ funs2 +' : undefined)');

                    }   
                }
            
            
           
        } else if (exp.kind == 'bop') {
            if (exp.op == '==') {
                expresion = ' Math.abs((' + this.generarExpresionDisc(exp.exp1,grap,ListnameF) + ') - (' + this.generarExpresionDisc(exp.exp2,grap,ListnameF) + ')) == 0 ';
                if(exp.exp1.kind == 'lit'){
                    this.valores.push(exp.exp1.val)
                }
                if(exp.exp2.kind == 'lit'){
                    this.valores.push(exp.exp2.val)
                }
                funcione.push(expresion)

            } else if (exp.op == '/=') {
                expresion = ' Math.abs((' + this.generarExpresionDisc(exp.exp1,grap,ListnameF) + ') - (' + this.generarExpresionDisc(exp.exp2,grap,ListnameF) + ')) != 0';
                funcione.push(expresion)

            } else if (exp.op == '^') {
                expresion = ' Math.pow(' + this.generarExpresionDisc(exp.exp1,grap,ListnameF) + ',' + this.generarExpresionDisc(exp.exp2,grap,ListnameF) + ') ';
                funcione.push(expresion)

            } else if (exp.op == '&&'){
                
                for (var atr of this.generarExpresionDisc(exp.exp1,grap,ListnameF)){
                    funcione.push(atr)
                }
                for (var atr2 of this.generarExpresionDisc(exp.exp2,grap,ListnameF)){
                    funcione.push(atr2)
                }
            }else if (exp.op == 'or'){
                funcione.push('!'+this.generarExpresionDisc(exp.exp1,grap,ListnameF))
                funcione.push('!'+this.generarExpresionDisc(exp.exp2,grap,ListnameF))
            }else{
                var lis1 = this.generarExpresionDisc(exp.exp1,grap,ListnameF)
                var lis2 = this.generarExpresionDisc(exp.exp2,grap,ListnameF)
                for(var expes1 of lis1){
                    for (var expes2 of lis2){
                        expresion = ' ((' + expes1 + ')' + exp.op + '(' + expes2 + ') )';
                        funcione.push(expresion)
                    }
                }


            }
        } else if (exp.kind == 'uop') {
            expresion = ' ' + exp.op + ' ' + this.generarExpresionDisc(exp.exp,grap,ListnameF) + ' ';
            funcione.push(expresion)

        } else if (exp.kind == 'app') {

			if (exp.fun == 'Math.cos') {
                exp.fun = 'Math.cos'
                expresion = ' ' + exp.fun + '(' + exp.args.map(e => this.generarExpresionDisc(e,grap,ListnameF)).join() + ') ';
                funcione.push(expresion)

			} else if (exp.fun == 'Math.sin') {
                exp.fun = 'Math.sin'
                expresion = ' ' + exp.fun + '(' + exp.args.map(e => this.generarExpresionDisc(e,grap,ListnameF)).join() + ') ';
                funcione.push(expresion)

			} else if (exp.fun == 'Math.round') {
                exp.fun = 'Math.round'
                expresion = ' ' + exp.fun + '(' + exp.args.map(e => this.generarExpresionDisc(e,grap,ListnameF)).join() + ') ';
                funcione.push(expresion)

leonelrosano's avatar
leonelrosano committed
1438
1439
1440
1441
1442
1443
            } else if (exp.fun == 'Math.sqrt') {
                exp.fun = 'Math.sqrt'
                expresion = ' ' + exp.fun + '(' + exp.args.map(e => this.generarExpresionDisc(e,grap,ListnameF)).join() + ') ';
                funcione.push(expresion)
    
            }else{
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481