Created
December 1, 2023 06:50
-
-
Save Eduar2TC/69acfce8a091e840341ae95fbaf93c54 to your computer and use it in GitHub Desktop.
3d tetraedro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'dart:math'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
body: Center( | |
child: AnimatedTetrahedron(), | |
), | |
), | |
); | |
} | |
} | |
class AnimatedTetrahedron extends StatefulWidget { | |
@override | |
_AnimatedTetrahedronState createState() => _AnimatedTetrahedronState(); | |
} | |
class _AnimatedTetrahedronState extends State<AnimatedTetrahedron> | |
with SingleTickerProviderStateMixin { | |
late AnimationController _controller; | |
late Animation<double> _animation; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = AnimationController( | |
vsync: this, | |
duration: Duration(seconds: 10), | |
)..repeat(); | |
_animation = Tween(begin: 0.0, end: -pi*2) | |
.animate(CurvedAnimation(parent: _controller, curve: Curves.linear)); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return AnimatedBuilder( | |
animation: _animation, | |
builder: (context, child) { | |
return Center( | |
child: Transform( | |
transform: Matrix4.rotationZ(_animation.value), | |
alignment: Alignment.center, | |
child: CustomPaint( | |
painter: TetrahedronPainter(_animation.value), | |
size: Size(300, 300), | |
), | |
), | |
); | |
}, | |
); | |
} | |
@override | |
void dispose() { | |
_controller.dispose(); | |
super.dispose(); | |
} | |
} | |
class TetrahedronPainter extends CustomPainter { | |
final double animation; | |
TetrahedronPainter(this.animation); | |
@override | |
void paint(Canvas canvas, Size size) { | |
final bluePaint = Paint()..color = Colors.blue..style=PaintingStyle.fill; | |
final redPaint = Paint()..color = Colors.red..style=PaintingStyle.fill; | |
final greenPaint = Paint()..color = Colors.green..style = PaintingStyle.fill; | |
final h = size.height; | |
final w = size.width; | |
final Offset c = Offset(w/2, h*0.6); | |
final double len = h * 0.3; | |
final topPoint = c + Offset(0 , -len); | |
final leftPoint = _getCoordinate(c, len, animation); | |
final rightPoint = _getCoordinate(c, len, animation + pi*2/3); | |
final bottomPoint = _getCoordinate(c, len, animation + pi*4/3); | |
final Path topFace = _createFace(topPoint, leftPoint, rightPoint); | |
final Path leftFace = _createFace(topPoint, leftPoint, bottomPoint); | |
final Path rightFace = _createFace(topPoint, rightPoint, bottomPoint); | |
canvas.drawPath(topFace, bluePaint); | |
canvas.drawPath(leftFace, redPaint); | |
canvas.drawPath(rightFace, greenPaint); | |
} | |
@override | |
bool shouldRepaint(TetrahedronPainter old) => true; | |
Offset _getCoordinate(Offset c, double len, double angle){ | |
return c + Offset( | |
len * cos(angle), | |
-len * sin(angle) | |
); | |
} | |
Path _createFace(Offset p1, Offset p2, Offset p3){ | |
final face = Path(); | |
face.moveTo(p1.dx, p1.dy); | |
face.lineTo(p2.dx, p2.dy); | |
face.lineTo(p3.dx, p3.dy); | |
face.close(); | |
return face; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment