From e6a200b09e9a38baac4bde53b68d3def6d2d350c Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Thu, 14 Feb 2019 10:55:57 +0100 Subject: [PATCH] Pcbnew: avoid integer overflow when displaying local coordinates. Minor cleanup in code. --- eeschema/sch_base_frame.cpp | 17 +++++++-------- pcbnew/pcb_base_frame.cpp | 40 +++++++++++++++-------------------- pcbnew/tools/drawing_tool.cpp | 2 +- 3 files changed, 26 insertions(+), 33 deletions(-) diff --git a/eeschema/sch_base_frame.cpp b/eeschema/sch_base_frame.cpp index e3ae88ce90..741ccaa593 100644 --- a/eeschema/sch_base_frame.cpp +++ b/eeschema/sch_base_frame.cpp @@ -222,7 +222,6 @@ void SCH_BASE_FRAME::SetTitleBlock( const TITLE_BLOCK& aTitleBlock ) void SCH_BASE_FRAME::UpdateStatusBar() { wxString line; - int dx, dy; BASE_SCREEN* screen = GetScreen(); if( !screen ) @@ -246,18 +245,18 @@ void SCH_BASE_FRAME::UpdateStatusBar() switch( GetUserUnits() ) { case INCHES: - absformatter = wxT( "X %.3f Y %.3f" ); - locformatter = wxT( "dx %.3f dy %.3f dist %.3f" ); + absformatter = "X %.3f Y %.3f"; + locformatter = "dx %.3f dy %.3f dist %.3f"; break; case MILLIMETRES: - absformatter = wxT( "X %.2f Y %.2f" ); - locformatter = wxT( "dx %.2f dy %.2f dist %.2f" ); + absformatter = "X %.2f Y %.2f"; + locformatter = "dx %.2f dy %.2f dist %.2f"; break; case UNSCALED_UNITS: - absformatter = wxT( "X %f Y %f" ); - locformatter = wxT( "dx %f dy %f dist %f" ); + absformatter = "X %f Y %f"; + locformatter = "dx %f dy %f dist %f"; break; case DEGREES: @@ -269,8 +268,8 @@ void SCH_BASE_FRAME::UpdateStatusBar() SetStatusText( line, 2 ); // Display relative coordinates: - dx = GetCrossHairPosition().x - screen->m_O_Curseur.x; - dy = GetCrossHairPosition().y - screen->m_O_Curseur.y; + double dx = (double)GetCrossHairPosition().x - (double)screen->m_O_Curseur.x; + double dy = (double)GetCrossHairPosition().y - (double)screen->m_O_Curseur.y; dXpos = To_User_Unit( GetUserUnits(), dx ); dYpos = To_User_Unit( GetUserUnits(), dy ); diff --git a/pcbnew/pcb_base_frame.cpp b/pcbnew/pcb_base_frame.cpp index 7e0fa417f0..36e2c2b16f 100644 --- a/pcbnew/pcb_base_frame.cpp +++ b/pcbnew/pcb_base_frame.cpp @@ -830,10 +830,6 @@ void PCB_BASE_FRAME::UpdateStatusBar() if( !screen ) return; - int dx; - int dy; - double dXpos; - double dYpos; wxString line; wxString locformatter; auto displ_opts = (PCB_DISPLAY_OPTIONS*)GetDisplayOptions(); @@ -842,27 +838,25 @@ void PCB_BASE_FRAME::UpdateStatusBar() if( displ_opts->m_DisplayPolarCood ) // display polar coordinates { - double theta, ro; + double dx = (double)GetCrossHairPosition().x - (double)screen->m_O_Curseur.x; + double dy = (double)GetCrossHairPosition().y - (double)screen->m_O_Curseur.y; - dx = GetCrossHairPosition().x - screen->m_O_Curseur.x; - dy = GetCrossHairPosition().y - screen->m_O_Curseur.y; + double theta = ArcTangente( -dy, dx ) / 10; + double ro = hypot( dx, dy ); - theta = ArcTangente( -dy, dx ) / 10; - - ro = hypot( dx, dy ); wxString formatter; switch( GetUserUnits() ) { case INCHES: - formatter = wxT( "r %.6f theta %.1f" ); + formatter = "r %.6f theta %.1f"; break; case MILLIMETRES: - formatter = wxT( "r %.6f theta %.1f" ); + formatter = "r %.6f theta %.1f"; break; case UNSCALED_UNITS: - formatter = wxT( "r %f theta %f" ); + formatter = "r %f theta %f"; break; case DEGREES: @@ -876,8 +870,8 @@ void PCB_BASE_FRAME::UpdateStatusBar() } // Display absolute coordinates: - dXpos = To_User_Unit( GetUserUnits(), GetCrossHairPosition().x ); - dYpos = To_User_Unit( GetUserUnits(), GetCrossHairPosition().y ); + double dXpos = To_User_Unit( GetUserUnits(), GetCrossHairPosition().x ); + double dYpos = To_User_Unit( GetUserUnits(), GetCrossHairPosition().y ); // The following sadly is an if Eeschema/if Pcbnew wxString absformatter; @@ -885,18 +879,18 @@ void PCB_BASE_FRAME::UpdateStatusBar() switch( GetUserUnits() ) { case INCHES: - absformatter = wxT( "X %.6f Y %.6f" ); - locformatter = wxT( "dx %.6f dy %.6f dist %.4f" ); + absformatter = "X %.6f Y %.6f"; + locformatter = "dx %.6f dy %.6f dist %.4f"; break; case MILLIMETRES: - absformatter = wxT( "X %.6f Y %.6f" ); - locformatter = wxT( "dx %.6f dy %.6f dist %.3f" ); + absformatter = "X %.6f Y %.6f"; + locformatter = "dx %.6f dy %.6f dist %.3f"; break; case UNSCALED_UNITS: - absformatter = wxT( "X %f Y %f" ); - locformatter = wxT( "dx %f dy %f dist %f" ); + absformatter = "X %f Y %f"; + locformatter = "dx %f dy %f dist %f"; break; case DEGREES: @@ -910,8 +904,8 @@ void PCB_BASE_FRAME::UpdateStatusBar() if( !displ_opts->m_DisplayPolarCood ) // display relative cartesian coordinates { // Display relative coordinates: - dx = GetCrossHairPosition().x - screen->m_O_Curseur.x; - dy = GetCrossHairPosition().y - screen->m_O_Curseur.y; + double dx = (double)GetCrossHairPosition().x - (double)screen->m_O_Curseur.x; + double dy = (double)GetCrossHairPosition().y - (double)screen->m_O_Curseur.y; dXpos = To_User_Unit( GetUserUnits(), dx ); dYpos = To_User_Unit( GetUserUnits(), dy ); diff --git a/pcbnew/tools/drawing_tool.cpp b/pcbnew/tools/drawing_tool.cpp index 4c271ab2af..c118c1ea21 100644 --- a/pcbnew/tools/drawing_tool.cpp +++ b/pcbnew/tools/drawing_tool.cpp @@ -1826,4 +1826,4 @@ PCB_LAYER_ID DRAWING_TOOL::getDrawingLayer() const } -const unsigned int DRAWING_TOOL::WIDTH_STEP = 100000; +const unsigned int DRAWING_TOOL::WIDTH_STEP = Millimeter2iu( 0.1 );