Rotate() method failing to rotate 3D cylinder
Posted: Mon Jun 03, 2024 4:48 pm
I am trying to create a 3D TeeChart that contains a Cylinder in the center. I want said cylinder to rotate around in the direction the mouse moves every time the user holds left click and drags. I want the cylinders center to remain the same while everything else rotates around it if that makes any sense. I am trying to use the Rotate() method to achieve this however my cylinder is staying still. Here is the current code that I have:
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using Steema.TeeChart.WPF;
using Steema.TeeChart.WPF.Styles;
using Steema.TeeChart.WPF.Drawing;
namespace tubo_MCB_TCP.GUI.Instruments
{
public partial class GraphingFor3D : Window
{
private bool isRotating = false;
private Point previousMousePosition;
private double cylinderRotation = 0;
public GraphingFor3D()
{
InitializeComponent();
InitializeTeeChart();
}
private void InitializeTeeChart()
{
try
{
// Configure TeeChart settings
TeeChart.Panel.Color = Colors.Black;
TeeChart.Aspect.View3D = true;
TeeChart.Aspect.Chart3DPercent = 100;
TeeChart.Aspect.Zoom = 60;
TeeChart.Chart.Header.Visible = false;
TeeChart.Axes.Top.Visible = false;
// Subscribe to the AfterDraw event
TeeChart.AfterDraw += TeeChart_AfterDraw;
// Set titles for X, Y, and Z axes
TeeChart.Axes.Bottom.Title.Text = "X Axis";
TeeChart.Axes.Bottom.Title.Font.Color = Colors.White;
TeeChart.Axes.Bottom.AxisPen.Color = Colors.White;
TeeChart.Axes.Bottom.Automatic = false;
TeeChart.Axes.Bottom.Visible = true;
TeeChart.Axes.Left.Title.Text = "Y Axis";
TeeChart.Axes.Left.Title.Font.Color = Colors.White;
TeeChart.Axes.Left.Automatic = false;
TeeChart.Axes.Left.AxisPen.Color = Colors.White;
TeeChart.Axes.Left.Visible = true;
TeeChart.Axes.Depth.Title.Text = "Z Axis";
TeeChart.Axes.Depth.Title.Font.Color = Colors.White;
TeeChart.Axes.Depth.AxisPen.Color = Colors.White;
TeeChart.Axes.Depth.Automatic = false;
TeeChart.Axes.Depth.Visible = true;
TeeChart.MouseLeftButtonDown += TeeChart_MouseLeftButtonDown;
TeeChart.MouseLeftButtonUp += TeeChart_MouseLeftButtonUp;
TeeChart.MouseMove += TeeChart_MouseMove;
TeeChart.Invalidate();
}
catch (Exception ex)
{
// Handle the exception (log or display it)
MessageBox.Show($"An error occurred: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void TeeChart_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
isRotating = true;
previousMousePosition = e.GetPosition(TeeChart);
Console.WriteLine("Mouse left button down");
}
private void TeeChart_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
isRotating = false;
Console.WriteLine("Mouse left button up");
}
private void TeeChart_MouseMove(object sender, MouseEventArgs e)
{
if (isRotating)
{
Point currentMousePosition = e.GetPosition(TeeChart);
Vector delta = currentMousePosition - previousMousePosition;
cylinderRotation = (cylinderRotation + delta.X) % 360;
Console.WriteLine($"Cylinder Rotation: {cylinderRotation}");
previousMousePosition = currentMousePosition;
TeeChart.InvalidateVisual(); // Force the TeeChart to redraw
}
}
// Event handler for drawing the cylinder
private void TeeChart_AfterDraw(object sender, Steema.TeeChart.WPF.Drawing.Graphics3D g)
{
// Define the center and size of the cylinder
int centerX = (int)(TeeChart.Chart.ChartBounds.Width / 2);
int centerY = (int)(TeeChart.Chart.ChartBounds.Height / 2);
int centerZ = 50; // Adjust Z-axis translation as needed
int width = 400; // Modify as needed
int height = 200; // Modify as needed
// Translate to the center of the cylinder
g.Translate(centerX, centerY, centerZ); // Adjust for Z-axis translation
// Apply rotation to the graphics object around the Z-axis
g.Rotate((int)cylinderRotation);
// Define the rectangle representing the base of the cylinder
Rect cylinderRect = new Rect(centerX - width / 2, centerY - height / 2, width, height);
// Draw the cylinder
g.Cylinder(false, cylinderRect, 10, 50, true);
g.Invalidate();
}
}
}
Here is my XAML in case that it is needed as well:
<Window x:Class="tubo_MCB_TCP.GUI.Instruments.GraphingFor3D"
xmlns="http://schemas.microsoft.com/winfx/2006 ... esentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/marku ... ility/2006"
xmlns:local="clr-namespace:tubo_MCB_TCP.GUI.Instruments"
xmlns:chart="clr-namespace:Steema.TeeChart.WPF;assembly=TeeChart.WPF"
xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:drawing="clr-namespace:System.Drawing;assembly=System.Drawing"
mc:Ignorable="d"
Title="GraphingFor3D" Height="634" Width="856">
<Grid>
<chart:TChart Name="TeeChart" HorizontalAlignment="Left" Height="531" Margin="83,10,0,0" VerticalAlignment="Top" Width="680"/>
</Grid>
</Window>
I have tried using a matrix to move it, using the Transform() method, but that would fully move my cylinder instead of rotating it in place. Am I missing something to my current code, I know 3D charts in itself can't be rotated, but I thought that it was possible to rotate a cylinder(), please let me know if you would like more information or if you need me to explain myself more, to better assist me. Thank you!
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using Steema.TeeChart.WPF;
using Steema.TeeChart.WPF.Styles;
using Steema.TeeChart.WPF.Drawing;
namespace tubo_MCB_TCP.GUI.Instruments
{
public partial class GraphingFor3D : Window
{
private bool isRotating = false;
private Point previousMousePosition;
private double cylinderRotation = 0;
public GraphingFor3D()
{
InitializeComponent();
InitializeTeeChart();
}
private void InitializeTeeChart()
{
try
{
// Configure TeeChart settings
TeeChart.Panel.Color = Colors.Black;
TeeChart.Aspect.View3D = true;
TeeChart.Aspect.Chart3DPercent = 100;
TeeChart.Aspect.Zoom = 60;
TeeChart.Chart.Header.Visible = false;
TeeChart.Axes.Top.Visible = false;
// Subscribe to the AfterDraw event
TeeChart.AfterDraw += TeeChart_AfterDraw;
// Set titles for X, Y, and Z axes
TeeChart.Axes.Bottom.Title.Text = "X Axis";
TeeChart.Axes.Bottom.Title.Font.Color = Colors.White;
TeeChart.Axes.Bottom.AxisPen.Color = Colors.White;
TeeChart.Axes.Bottom.Automatic = false;
TeeChart.Axes.Bottom.Visible = true;
TeeChart.Axes.Left.Title.Text = "Y Axis";
TeeChart.Axes.Left.Title.Font.Color = Colors.White;
TeeChart.Axes.Left.Automatic = false;
TeeChart.Axes.Left.AxisPen.Color = Colors.White;
TeeChart.Axes.Left.Visible = true;
TeeChart.Axes.Depth.Title.Text = "Z Axis";
TeeChart.Axes.Depth.Title.Font.Color = Colors.White;
TeeChart.Axes.Depth.AxisPen.Color = Colors.White;
TeeChart.Axes.Depth.Automatic = false;
TeeChart.Axes.Depth.Visible = true;
TeeChart.MouseLeftButtonDown += TeeChart_MouseLeftButtonDown;
TeeChart.MouseLeftButtonUp += TeeChart_MouseLeftButtonUp;
TeeChart.MouseMove += TeeChart_MouseMove;
TeeChart.Invalidate();
}
catch (Exception ex)
{
// Handle the exception (log or display it)
MessageBox.Show($"An error occurred: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void TeeChart_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
isRotating = true;
previousMousePosition = e.GetPosition(TeeChart);
Console.WriteLine("Mouse left button down");
}
private void TeeChart_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
isRotating = false;
Console.WriteLine("Mouse left button up");
}
private void TeeChart_MouseMove(object sender, MouseEventArgs e)
{
if (isRotating)
{
Point currentMousePosition = e.GetPosition(TeeChart);
Vector delta = currentMousePosition - previousMousePosition;
cylinderRotation = (cylinderRotation + delta.X) % 360;
Console.WriteLine($"Cylinder Rotation: {cylinderRotation}");
previousMousePosition = currentMousePosition;
TeeChart.InvalidateVisual(); // Force the TeeChart to redraw
}
}
// Event handler for drawing the cylinder
private void TeeChart_AfterDraw(object sender, Steema.TeeChart.WPF.Drawing.Graphics3D g)
{
// Define the center and size of the cylinder
int centerX = (int)(TeeChart.Chart.ChartBounds.Width / 2);
int centerY = (int)(TeeChart.Chart.ChartBounds.Height / 2);
int centerZ = 50; // Adjust Z-axis translation as needed
int width = 400; // Modify as needed
int height = 200; // Modify as needed
// Translate to the center of the cylinder
g.Translate(centerX, centerY, centerZ); // Adjust for Z-axis translation
// Apply rotation to the graphics object around the Z-axis
g.Rotate((int)cylinderRotation);
// Define the rectangle representing the base of the cylinder
Rect cylinderRect = new Rect(centerX - width / 2, centerY - height / 2, width, height);
// Draw the cylinder
g.Cylinder(false, cylinderRect, 10, 50, true);
g.Invalidate();
}
}
}
Here is my XAML in case that it is needed as well:
<Window x:Class="tubo_MCB_TCP.GUI.Instruments.GraphingFor3D"
xmlns="http://schemas.microsoft.com/winfx/2006 ... esentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/marku ... ility/2006"
xmlns:local="clr-namespace:tubo_MCB_TCP.GUI.Instruments"
xmlns:chart="clr-namespace:Steema.TeeChart.WPF;assembly=TeeChart.WPF"
xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:drawing="clr-namespace:System.Drawing;assembly=System.Drawing"
mc:Ignorable="d"
Title="GraphingFor3D" Height="634" Width="856">
<Grid>
<chart:TChart Name="TeeChart" HorizontalAlignment="Left" Height="531" Margin="83,10,0,0" VerticalAlignment="Top" Width="680"/>
</Grid>
</Window>
I have tried using a matrix to move it, using the Transform() method, but that would fully move my cylinder instead of rotating it in place. Am I missing something to my current code, I know 3D charts in itself can't be rotated, but I thought that it was possible to rotate a cylinder(), please let me know if you would like more information or if you need me to explain myself more, to better assist me. Thank you!