A Bronx teen who twice pointed his gun at cops and pulled the trigger — only to realize that he’d just emptied the weapon shooting out apartment windows — nearly hit several residents with his gunfire, police sources and witnesses said Sunday.

Raymond Leonardo, 17, had wildly fired six shots into a building on East 140th Street in Mott Haven around 9 p.m. Saturday before he aimed the gun at responding officers who showed up to answer a 911 call, police said.

“My son came out and said he almost got shot,” said Maretta Carter, who was cooking inside her fourth-floor apartment when a bullet came smashing through her window. “He was just cleaning his room.”

“I’m scared, and I want to move because it can happen again and be even worse,” she said.

A resident on the second floor who declined to give his name said he heard several gunshots before one of the bullets hit his window frame.

“We were in the living room when it happened, so nobody got hurt,” the man said. “I heard the shot and saw the police run up the block.”

The shooting occurred on the same day that two Brooklyn officers were killed execution-style by a cop-hating psycho.

After firing at the building, Leonardo was chased a couple blocks before he turned and pointed his .357-caliber revolver at officers standing just feet from him and tried to squeeze off two rounds — but the gun was empty, authorities said.

Leonardo was arrested and faces several gun and menacing charges.